force php strtotime to use UTC
Asked Answered
M

3

13

I've seen a few questions about this but not a clear answer... strtotime() will use the default timezone set for PHP when converting the string to a unix timestamp.

However, I want to convert a string to unix timestamp in UTC. Since there is no parameter for doing so, how can this be done?

The string I'm trying to convert is: 2011-10-27T20:23:39, which is already in UTC. I want to represent that as a unix timestamp also in UTC.

Thank you

Makeweight answered 28/10, 2011 at 2:58 Comment(1)
Also consider using new DateTimeZone('UTC');, see https://mcmap.net/q/903436/-importing-ics-into-google-calendar-with-correct-timezoneStupa
B
10

Set the system default timezone before making the strtotime call:

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's 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().

See it in action.

Backstitch answered 28/10, 2011 at 3:42 Comment(2)
Would that cause issues with the rest of the script though? I guess I could set it back afterwards?Makeweight
@Brian: It could cause issues, yes. But as long as you set it back there will be no problem.Backstitch
B
23

I realize this question is very old, but I found another option that can be helpful. Instead of setting and then reverting php's time zone, you can specify the timezone as a part of the string you pass to strtotime like so:

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

Note: I am on PHP 5.5.9, but this should work in any php version.

Bellwether answered 28/1, 2015 at 20:22 Comment(1)
This is easier but you have to watch out for user input. If you just sending in what a user has typed you never know if they are going to include the timezone or something that will mess the string up.Chatham
B
10

Set the system default timezone before making the strtotime call:

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's 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().

See it in action.

Backstitch answered 28/10, 2011 at 3:42 Comment(2)
Would that cause issues with the rest of the script though? I guess I could set it back afterwards?Makeweight
@Brian: It could cause issues, yes. But as long as you set it back there will be no problem.Backstitch
D
5

Both other answers are really fine. As I have the same issue, I want to offer a third option.

$utc = new DateTimeZone('UTC');
$dt = new DateTime('2011-10-27T20:23:39', $utc);
var_dump($dt->format('U'));

gives string(10) "1319747019"

Deci answered 25/10, 2018 at 7:39 Comment(3)
This does not work if there is a timezone present in the datetime string.Reinhart
Maybe, but this is a solution for the question. Additionally, what's different from the other solutions!? In all other cases, a present timezone can lead to unwanted result.Deci
Using DateTimeZone as Parameter suggests that it would always use this timezone.Reinhart

© 2022 - 2024 — McMap. All rights reserved.