views:

154

answers:

3

Whilst working on a UI update for a client, I noticed the dates associated with all the articles were out by a day. I figured I'd screwed something up during my changes, but to be sure, threw together a little php test file that gave me some odd results. The test file is just;

<?php
$date = 1246053600;
echo 'unix: ',$date,', converted: ',date('d/m/Y', $date);
?>

If I run the above code on my localhost I get:

unix: 1246053600, converted: 26/06/2009

But if I run it on the production server I get:

unix: 1246053600, converted: 27/06/2009

Notice the day difference between the two? What's happening here?! Surely converting a unix timestamp to a date doesn't have any server specific dependancies?

+1  A: 

I had a similar problem before. I found that the time zone in the php.ini from my development machine was different from the production server.

I'd say it is worth checking that out.

Marcel Tjandraatmadja
+4  A: 

Your servers might be set to two different time zones, and they're interpreting the timestamp as the number of seconds since midnight january 1st 1970 GMT. The dates may not be off by a whole day, but just part of a day, enough to make it cross over the midnight boundary.

Breton
It turned out to be exactly this; the timezone settings on the production server *just* bumped the date into the next day.
MatW
+4  A: 

The issue is that the $date value you are providing is presumed to be in UTC. If you use gmdate, you will get the same result on both servers. Otherwise, the date and time displayed will be adjusted according to the servers' timezone. You can use the O (capital oh) code to print out the timezone to make the current setting on each server clear:

echo 'unix: ',$date,', converted: ',date('d/m/Y O', $date);

Conspicuous Compiler
+1 for the intro to gmdate(); very handy!
MatW