Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why strtotime(1st January 1970) returns -3600 instead of 0 in PHP? [duplicate]

Tags:

php

datetime

Am using strtotime to get seconds past 1 January 1970, but am not understanding that why am getting -3600 if the clock is set to 12.00AM and I get 0 when the clock is set to 1 AM, so what's with that 1 HOUR? Is it an issue with the timezone?

echo 'I Expect 0 Here '.strtotime('1st January 1970').'<br />';
//This gives me -3600

echo 'I Expect 3600 Here '.strtotime('1st January 1970 01.00AM'); 
//This gives me 0

P.S I've not set any Time Zone in my PHP file or I've not even modified my .ini file (Fresh Installed XAMPP)

Update : Time-Zone : Europe/Berlin

like image 260
Mr. Alien Avatar asked Apr 03 '13 08:04

Mr. Alien


2 Answers

It's most likely due to your local time zone.
What's the output of

var_dump(date_default_timezone_get());

on that machine?
You can also check the difference between mktime and gmmktime

echo " mktime: ", mktime(), "\n";
echo " gmmktime: ", gmmktime(), "\n";
like image 158
VolkerK Avatar answered Nov 10 '22 10:11

VolkerK


Your timezone must be set to UTC+1. Midnight in your timezone happened an hour before midnight in UTC, which explains the offset of -3600 seconds.

You can set the timezone to UTC to get the expected result:

date_default_timezone_set('UTC');
like image 33
Joni Avatar answered Nov 10 '22 08:11

Joni