Strange behaviour of date()

Permanent Link: Strange behaviour of date() 2. Januar 2009 RSS Feed for comments on RSS-Feed für Kommentare zu: Strange behaviour of date() comments feed

Recently I used time and date functions to calculate an ETA and remaining running time for a longer running script. When showing the remaining time using the date() function I encountered some strange behaviour:

According to the documentation date() expects the second parameter to be a UNIX Timestamp, which - as we all know - starts at 01.01.1970 00:00:00. Meaning when you print out date('H:i', 0) you'd expect to get 00:00. For some reason this is not the case:

php > print date('H:i', 0);
01:00

Does anyone know why this is the case? In my script I did a workaround by simply substracting one hour, which isn't the best style obviously.

(Note: I used the php interactive shell you can run by executing php -a to print out the date)

4 comments

Ralf Eggerts Gravatar

Ralf Eggert
02.01.2009, 18:41 o'clock

Just a wild guess: check your time zone settings. Just play around with date_default_timezone_set() and check results again.

greetinx,

Ralf

Dominik Jungowskis Gravatar

Dominik Jungowski
08.01.2009, 11:18 o'clock

Well, we've tried it on several servers and always got the same result. The datetime was ok, according to the server settings. We haven't tried date_default_timezone_set() though, gonna try it out soon.

Johannes Schlüters Gravatar

Johannes Schlüter
23.01.2009, 01:57 o'clock

date expects a Unix Time Stamp, Unix time stamps are counted from 1970/01/01 00:00:00 GMT (!), Germany is GMT+1

Dominik Jungowskis Gravatar

Dominik Jungowski
31.01.2009, 12:26 o'clock

@Ralf: Well, the timezone is correct:
php > print(date_default_timezone_get());
Europe/Berlin

@Johannes: That seems reasonable, thanks!

Write a comment

(will not be published)