date - PHP Session variable - find time difference -


the program trying write simple enough, find difference between 2 times: today's, , time user logs in put in session variable.

the session variable

$_session['logintime']; 

is set today's date

$_session['logintime'] = date('y-m-d h:i:s'); 

when user logs out duration have been logged in found using code on separate page:

if(isset($_session['logintime'])) {     $logtime = $_session['logintime'];      $duration = strtotime(date('y-m-d h:i:s')) - strtotime($logtime);     $duration = date('h:i:s', $duration);  } 

if log in (22:15 19/04/2016) , stay logged in 1min 10 sec returns 01:01:10. cannot understand hour coming from, timezones set same.

  • the minutes , seconds calculated fine 1 hour added seemingly no reason

thanks reading! appreciated!

the datetime object friend.

from php manual, simple example follows:

$datetime1 = new datetime('2009-10-11'); $datetime2 = new datetime('2009-10-13'); $interval = $datetime1->diff($datetime2); echo $interval->format('%r%a days'); 

you can adapt code this:

if(isset($_session['logintime'])) {     $logtime = new \datetime($_session['logintime']);     $currenttime = new \datetime('now');     $interval = $logtime->diff($currenttime);     $duration = $interval->format('%h:%i:%s'); } echo $duration; 

example output:

15:48:57


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -