php get amount of time since last login in seconds, minutes or hours

DrevanTonder

I want to get the amount of time since a user logged in.

php:

$sql = "SELECT * FROM login WHERE username = '$user'" ;
$result = mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($result);
$lastOnline = $row["lastOnline"];    
if ($lastOnline >= $time-60 or $lastOnline == $time){
  $lastOnline = "Now";
} else if ($lastOnline >= $time-3600){
  $lastOnline = date('i', $lastOnline-$time)." minutes ago";
} else if ($lastOnline >= $time-86400){
  $lastOnline = date('G', $lastOnline-$time)." hours ago";
} else if ($lastOnline >= $time-604800){
  $lastOnline = date('j', $lastOnline-$time)." days ago";
}

The hours seem to work but not the minutes. It seems to show the opposite.

Hanky Panky

Your calculation is invalid, at least for the minutes case.

$lastOnline = date('i', $lastOnline-$time)." minutes ago";
                        ^^^^^^^^^^^^^^^^^

That will always return a Negative timestamp value because lastOnline is in the past and time is the current time(). It should be otherwise

$lastOnline = date('i', $time-$lastOnline)." minutes ago";
                        ^^^^^^^^^^^^^^^^^

Fiddle

Example

$lastOnline=time()-600;
$time=time();
$lastOnline = date('i', $time-$lastOnline)." minutes ago";
echo $lastOnline;

Output

10 minutes ago

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

get all the time in 15 minutes interval since last 4 hours

From Dev

Seconds, minutes and hours since a date

From Dev

Seconds, minutes and hours since a date

From Dev

Rails - Get the time difference in hours, minutes and seconds

From Dev

Elapsed time in seconds, minutes, hours

From Dev

PHP: convert seconds to minutes and hours

From Dev

Convert seconds to hours and minutes in real time

From Java

Adding hours, minutes, seconds to current time

From Dev

Xcode time difference in days, hours, minutes and seconds

From Dev

Convert seconds to hours and minutes in real time

From Dev

Split a time into hours value, minutes value, and seconds

From Dev

PHP Convert seconds into seconds, minutes, hours with hours exceeding 24

From Dev

Get current time as ulong amount of minutes since 01.01.2001 00:00 in c#

From Dev

PHP/ MySQL Milliseconds to Hours:Minutes:Seconds

From Dev

Get current time in hours and minutes

From Dev

Python time in format => [days:hours:minutes:seconds] to seconds

From Dev

Python Time convertor from seconds to days, hours, minutes & seconds

From Dev

get time in Hours, Min, Seconds (currently in seconds)

From Dev

Get time split into minutes and seconds

From Dev

get minutes and seconds of current time

From Dev

Google Tag manager: get the amount of time (minutes/seconds) it takes to complete progress

From Dev

How to write a method to get a value in seconds and print in Hours, Minutes and Seconds

From Dev

Convert Seconds to Hours and Minutes

From Dev

hours and minutes to seconds

From Dev

Convert hours:minutes to minutes:seconds

From Dev

converting seconds to hours,minutes, and seconds?

From Dev

Converting seconds to hours and minutes and seconds

From Dev

What is the best way to store the amount of time spent (hours : minutes) in MongoDb?

From Dev

How to get the time since midnight in seconds

Related Related

  1. 1

    get all the time in 15 minutes interval since last 4 hours

  2. 2

    Seconds, minutes and hours since a date

  3. 3

    Seconds, minutes and hours since a date

  4. 4

    Rails - Get the time difference in hours, minutes and seconds

  5. 5

    Elapsed time in seconds, minutes, hours

  6. 6

    PHP: convert seconds to minutes and hours

  7. 7

    Convert seconds to hours and minutes in real time

  8. 8

    Adding hours, minutes, seconds to current time

  9. 9

    Xcode time difference in days, hours, minutes and seconds

  10. 10

    Convert seconds to hours and minutes in real time

  11. 11

    Split a time into hours value, minutes value, and seconds

  12. 12

    PHP Convert seconds into seconds, minutes, hours with hours exceeding 24

  13. 13

    Get current time as ulong amount of minutes since 01.01.2001 00:00 in c#

  14. 14

    PHP/ MySQL Milliseconds to Hours:Minutes:Seconds

  15. 15

    Get current time in hours and minutes

  16. 16

    Python time in format => [days:hours:minutes:seconds] to seconds

  17. 17

    Python Time convertor from seconds to days, hours, minutes & seconds

  18. 18

    get time in Hours, Min, Seconds (currently in seconds)

  19. 19

    Get time split into minutes and seconds

  20. 20

    get minutes and seconds of current time

  21. 21

    Google Tag manager: get the amount of time (minutes/seconds) it takes to complete progress

  22. 22

    How to write a method to get a value in seconds and print in Hours, Minutes and Seconds

  23. 23

    Convert Seconds to Hours and Minutes

  24. 24

    hours and minutes to seconds

  25. 25

    Convert hours:minutes to minutes:seconds

  26. 26

    converting seconds to hours,minutes, and seconds?

  27. 27

    Converting seconds to hours and minutes and seconds

  28. 28

    What is the best way to store the amount of time spent (hours : minutes) in MongoDb?

  29. 29

    How to get the time since midnight in seconds

HotTag

Archive