date() returning wrong day although the timestamp is correct!
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 s开发者_运维知识库erver (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?
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?
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');
It's hard to be certain here, but could you problem be caused by a daylight saving time transition in the remote server's timezone?
In most countries the transition usually happens on weekends and, since the day is extended by an hour, this is one case where adding 86400 seconds to a time value would not return the date after that.
Run your code with the starting date +1 hr and then -1hr and see what results you get. You'll get more clues and most likely it is to do with Daylight Saving Time.
Also, as pointed out by Pekka, try the same with the date set to Oct 31st and see what happens.
Good question.
精彩评论