I want to create two equal timestamps for a given day in PHP and Javascript, but there’s a difference….
1 July 2012:
PHP
$test = mktime(0,0,0,7,1,2012); gives 1341093600
JavaScript
var d = Date.UTC(2012,6,1);
var timestamp = d/1000; gives 1341100800
Why the 2 hour difference?
The call to
Date.UTCwill create a timestamp that is midnight on 2012-07-01 UTC.mktime, on the other hand, will do the same thing except for whatever timezone PHP is configured to use.
In other words, your server isn’t set to UTC.
The easiest solution is probably strtotime:
There’s also gmmktime or you could set the timezone of PHP to UTC. I would do it in the
.ini, but to do it in code you could use date_default_timezone_set:As a note of personal preference, I avoid mktime. Between
strtotimeandDateTime, I very rarely find cases in which mktime results in cleaner, more readable code. The beastly parameter list of mktime makes any line it’s in appear plain frightening.