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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

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

Post 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?
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Sounds like a time zone issue.
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Time zones actually do matter, as it's actually "seconds since midnight of January 1, 1970 in Greenwich."
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post by Josh1billion »

Ahh I see.. so mktime() takes the server's timezone setting into account?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It has to, yes.
Post Reply