-1

I update user activity time in activity db table field name "activity" as 2021-03-05 06:32:46 so supposing if current time now is 2021-03-05 06:32:46 and user is idle then how can i show it on page like a timer 00:01:58 and it keeps increasing till the user activity column is not updated.

just need to show h m s in php from mysql timestamp table

really appreciate your help

Thanks

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
rosemary
  • 3
  • 4
  • I can think of a number of ways: put the last-activity in a html attribute and start some javascript that inceases the value every second for example, you could do some ajax polling for the last-activity and show the difference or you could do something similar with websockets; also look at [this SO post](https://stackoverflow.com/questions/29971898/how-to-create-an-accurate-timer-in-javascript) – zedling Mar 05 '21 at 12:09
  • @zedling i need it just php not js as i refresh the time in js but just need help on how i get the time difference like mentioned above – rosemary Mar 05 '21 at 12:10

1 Answers1

0

if you only need to calculate the difference between two dates than this might do it:

// insert the last value of the activity as the second param
$a = \DateTime::createFromFormat('Y-m-d H:i:s', '2021-03-04 07:56:34');
// defaults to current time
$b = new \DateTime();

// calculate the difference (time elapsed)
$diff = $b->diff($a);

// print it anywhere
echo $diff->format('%H:%I:%S');

Note: this will only show the elapsed time at the calculation, and won't increase by itself; you will need something client-side like Javascript-ajax or websockets for that

for more info see: DateIntervals DateTime

zedling
  • 638
  • 8
  • 28