tags:

views:

53

answers:

2

Hi

If I run the following in PHP:

echo mktime(0,0,0,1,1,1970);

the returned value is -3600, not 0 as I expected.

The server is UK based, it's currently 21 Sep (i.e. BST summertime) (though I wouldn't expect this to affect the epoch timestamp) and per php.info: "Default timezone Europe/London".

Setting the daylight saving time flag also, as follows, gives:

echo mktime(0,0,0,1,1,1970,0); (i.e. the correct DST flag, 0 as 1 Jan not DST/BST) returns -3600

echo mktime(0,0,0,1,1,1970,1); (the incorrect flag - setting 1 Jan as DST) returns -7200

echo mktime(0,0,0,1,1,1970,-1); (i.e. DST flag not set - left to PHP to decide) returns -3600

Does anyone know why the epoch would be returned as -3600, not 0, please?

+5  A: 

When it was midnight on Jan 1st 1970 in British Summer Time, it was one hour to midnight in Greenwich Mean Time. Try setting the time zone to UTC instead:

date_default_timezone_set('UTC'); // or just change php.ini
Hammerite
+1, a more detailed / better explanation then I gave.
Wrikken
Hi that's great, thank you. I'd thought it was obvious that 1 Jan 1970 had to be in GMT (i.e. not BST/DST - only in force late March - late October) and hence a unix timestamp of 0 - hadn't realised the date was interpreted as being in the same time zone as the script. I'm pleased I'm programming the script in BST, otherwise the error would have gone unnoticed until things stopped working in March!
PHP
... and just spotted this: http://en.wikipedia.org/wiki/British_Summer_Time - BST was in force during winter 1969/1970 as a trial
PHP
+1  A: 

mktime() is based on your current timezone. If you want to create a timestamp based on GMT you have to use the gmmktime() function.

gmmktime(0,0,0,1,1,1970)

code on ideone


Resources :

Colin Hebert
Thanks, useful!
PHP