Reputation: 33
I do
strtotime("2008-09-04")
Where 09 is the month and 04 is the day and I get the result:
1220500800
Which is Thu, 04 Sep 2008 04:00:00 GMT. Where does those 4 hours come from? I should get 1220486400
instead of 1220500800
from strtotime.
Upvotes: 2
Views: 3621
Reputation: 777
The most common problems with PHP's strtotime are timezones and date-time formats. I will address those 2 points.
So now we have the picture clear. To convert a date time into unixtimestamp do:
// Saves your local set Timezone.
$saveDDTZ = date_default_timezone_get();
// Sets the timezone to UTC
date_default_timezone_set("UTC");
// Converts a date-time into a unix timestamp (= 1560470400).
$unixTS = strtotime( "2019-06-14 00:00:00 UTC");
// Restore the timezone
date_default_timezone_set($saveDDTZ");
To restore a unix timestamp to a Date use:
// Saves your local set Timezone.
$saveDDTZ = date_default_timezone_get();
// Sets the timezone to UTC
date_default_timezone_set("UTC");|
$unixTS = 1560470400
$dateTime = date("Y-m-d H:i:s", $unixTS);
// Restore the timezone
date_default_timezone_set($saveDDTZ");
If you want to change the UTC date into a TimeZone use the difference of the UTC and the TimeZone and the Daylight Saving.
Upvotes: 0
Reputation: 43552
You can set timezone globally with function date_default_timezone_set('UTC');
, or you can just set timezone locally when you call strtotime()
function like:
echo strtotime("2008-09-04 +0000"); # 1220486400
Upvotes: 6
Reputation: 106
As people above have said, you're likely suffering from a time zone mismatch. This function may be of use in debugging the issue: https://www.php.net/date_default_timezone_get
Upvotes: 0