Page 1 of 1

Odd error with time -- are UNIX and Windows timestamps diff?

Posted: Thu Oct 25, 2007 9:13 pm
by Josh1billion
This is a very simple problem but a little difficult to explain. I think the cause is that I'm running the code on my Windows installation and also on my Linux webhost-- the time is the same but the timestamps (date("U")) are different-- the problem could be that Unix and Windows timestamps are different (if they are..). So is that the cause of the problem?

Here's the problem:

I'm using some time-related code, testing on both my local WAMP server and on my webhost. Running basically the same code to display the time (using an alternate version of date(), my own time_Date() function (seen in this thread-- it is practically the same as date() in usage-- this is irrelevant, but I have the offset at 0 on my local installation's time.php and around 6 hours on my webhost's time.php).

So I'm using this code below to display the time in two formats: a string and a timestamp. For our purposes in this example, time_Date() is exactly the same as date(), so you could assume that date() is being used in the code; I'm 99% sure the function itself is not at fault here.

Code: Select all

print time_Date("F j, Y") . " at " . time_Date("g:i:s a");
	print "<BR>" . time_Date("U");
Running on both servers, the string version of the time is the same but the timestamp is different, seen here:

Server 1 output (my local WAMP installation):

Code: Select all

October 25, 2007 at 9:05:20 pm
1193364320

Code: Select all

October 25, 2007 at 9:05:20 pm
1193371520
So the times are correct, but the timestamp for the server is two hours ahead of the timestamp of my local installation.

What's the deal?

Posted: Thu Oct 25, 2007 9:18 pm
by Josh1billion
<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>, okay, found it. As I had suspected, the Unix timestamps must be two hours faster than Windows ones for some reason.

Running this code on Windows (my local WAMP installation) and on Linux (my webhost) provided two different results:

Code: Select all

print mktime(0, 0, 0, 10, 31, 2007)
Why is that? Oh well, at least it's solved now. Still leaves me feeling weird though.

Posted: Sat Oct 27, 2007 3:47 pm
by califdon
Sounds like a time zone issue.

Posted: Sat Oct 27, 2007 4:32 pm
by Josh1billion
The timestamp, as I understand it, is defined as "seconds since January 1, 1970." Therefore, time zones shouldn't matter. I would expect the same value returned for mktime(0, 0, 0, 10, 31, 2007) (that's Oct. 31, 2007 at midnight) to be the same on any server by that definition.. but that's not the case, odd..

Oh well, not a big deal since I sort of hacked a solution together.

Posted: Sat Oct 27, 2007 4:40 pm
by feyd
Time zones actually do matter, as it's actually "seconds since midnight of January 1, 1970 in Greenwich."

Posted: Sat Oct 27, 2007 4:54 pm
by Josh1billion
Ahh I see.. so mktime() takes the server's timezone setting into account?

Posted: Sat Oct 27, 2007 5:13 pm
by feyd
It has to, yes.