Jump to content

[SOLVED] time and dates


emperornortonus

Recommended Posts

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

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

 

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!  ;D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.