emperornortonus Posted October 15, 2008 Share Posted October 15, 2008 Hey guys, I was trying to test out how PHP handles dates and time. What I wanted is a simple way to measure the amount of days:hours:minutes:seconds elapsed between two times. I figured I would test how PHP handles this by writing the following code: <?php $time1 = time(); // unix timestamp $time2 = time() + 24; //unix timestamp plus 24 seconds $time_diff = $time2 - $time1; //time between the two stamps (should be 24 seconds) echo $time_diff . '<br />'; // output the correct amount of elapsed seconds echo date("G:i:s", $time_diff) . '<br />'; // Gives wrong amount of hours elapsed! 19:00:24 ?> Here are the results I'm getting... " 24 19:00:24 " Shouldn't it read 00:00:24? Why does it add 19 hours to my elapsed time value? If you format 24 shouldn't it be 24 seconds? Link to comment https://forums.phpfreaks.com/topic/128479-solved-time-and-dates/ Share on other sites More sharing options...
iversonm Posted October 15, 2008 Share Posted October 15, 2008 because its getting the time of the Unix Epoch, or the beggining which is why its that time so the current timestamp is 1224045213 100 seconds ago the timestamp was 1224045113 so what the server is trying to do is get the timestamp when then time was 24 which was 24 seconds after the Unix Epoch Link to comment https://forums.phpfreaks.com/topic/128479-solved-time-and-dates/#findComment-665845 Share on other sites More sharing options...
Barand Posted October 15, 2008 Share Posted October 15, 2008 i get 24 1:00:24 I think it's a time-zone effect try echo gmdate() instead of date() Link to comment https://forums.phpfreaks.com/topic/128479-solved-time-and-dates/#findComment-665910 Share on other sites More sharing options...
emperornortonus Posted October 15, 2008 Author Share Posted October 15, 2008 reply to iversonm -- Yes, but 24 seconds after the unix epoch (January 1st 1970 at midnight (00:00:00)) Should still be 00:00:24, no? reply to Barand -- gmdate works perfect! Thanks so much Barand! I thought it might be a timezone issue, but you hit the nail on the head with gmdate. Thanks again! Link to comment https://forums.phpfreaks.com/topic/128479-solved-time-and-dates/#findComment-666271 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.