Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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 :

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top