Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date() returning wrong day although the timestamp is correct!

Tags:

php

datetime

I have a bizzare problem with php date function.

code:

$numDays = 8;
$date = strtotime('2010-11-06');
for ($i=1; $i<=$numDays; $i++)
{
    $thisDay = date("D, d M Y", $date);
    print ($thisDay.'<br>');
    $date+=86400; // add one day to timestamp
}

result on my server (local host, windows):

Sat, 06 Nov 2010

Sun, 07 Nov 2010

Mon, 08 Nov 2010

Tue, 09 Nov 2010

Wed, 10 Nov 2010

Thu, 11 Nov 2010

Fri, 12 Nov 2010

Sat, 13 Nov 2010

Result on my web server (linux)

Sat, 06 Nov 2010

*Sun, 07 Nov 2010

Sun, 07 Nov 2010*

Mon, 08 Nov 2010

Tue, 09 Nov 2010

Wed, 10 Nov 2010

Thu, 11 Nov 2010

Fri, 12 Nov 2010

Notice how Sun, 07 Nov 2010 appears twice on the remote server?? Why is this happening? can anyone explain this Behavior?

like image 413
mspir Avatar asked Apr 10 '10 12:04

mspir


2 Answers

November 7, 2010 is the DST switch date in many time zones (but not Greece where you seem to be located). From Wikipedia:

Starting in 2007, most of the United States and Canada observe DST from the second Sunday in March to the first Sunday in November, almost two-thirds of the year.

In Greece, it seems to be October 31. Which time zone do you have you set on your machine?

like image 94
Pekka Avatar answered Oct 08 '22 15:10

Pekka


It's good practice to do time calculations in UTC and then convert them to the required timezone for the user's location using PHPs datetime functions:

date_default_timezone_set('UTC');
$timezone = new DateTimeZone('Europe/Athens');
$datetime = new DateTime('now', $timezone);
echo $datetime->format('Y-m-d H:i:s');
like image 26
waraker Avatar answered Oct 08 '22 16:10

waraker