Force php strtotime use UTC

I saw a few questions about this, but the answer is not clear ... strtotime () will use the default timezone for PHP when converting a string to a unix timestamp.

However, I want to convert the string to timestamp unix to UTC. Since there are no parameters for this, how can this be done?

I am trying to convert a string: 2011-10-27T20: 23: 39, which is already in UTC. I want to present this as a unix timestamp also in UTC.

thank

+5
source share
2 answers

Set the default system time zone before calling strtotime:

date_default_timezone_set('UTC');
echo strtotime('2011-10-27T20:23:39')."\n";

// Proof:
print_r(getdate(strtotime('2011-10-27T20:23:39')));

// IMPORTANT: It possible that you will want to
// restore the previous system timezone value here.
// It would need to have been saved with
// date_default_timezone_get().

Look at the action .

+5
source

, , , . , php, , strtotime, :

echo date_default_timezone_get();
output: America/Chicago

echo gmdate('Y-m-d H:i:s', strtotime('2011-10-27T20:23:39'));
output: 2011-10-28 01:23:39

echo gmdate('Y-m-d H:i:s', strtotime('2011-10-27T20:23:39 America/Chicago'));
output: 2011-10-28 01:23:39

echo gmdate('Y-m-d H:i:s', strtotime('2011-10-27T20:23:39 UTC'));
output 2011-10-27 20:23:39

. PHP 5.5.9, php.

+10

All Articles