php convert datetime to UTC
Solution 1
Try the getTimezone and setTimezone, see the example
(But this does use a Class)
UPDATE:
Without any classes you could try something like this:
$the_date = strtotime("2010-01-19 00:00:00");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");
NOTE: You might need to set the timezone back to the original as well
Solution 2
Use strtotime to generate a timestamp from the given string (interpreted as local time) and use gmdate to get it as a formatted UTC date back.
Example
As requested, here’s a simple example:
echo gmdate('d.m.Y H:i', strtotime('2012-06-28 23:55'));
Solution 3
Using DateTime:
$given = new DateTime("2014-12-12 14:18:00");
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 14:18:00 Asia/Bangkok
$given->setTimezone(new DateTimeZone("UTC"));
echo $given->format("Y-m-d H:i:s e") . "\n"; // 2014-12-12 07:18:00 UTC
Solution 4
Do this way:
gmdate('Y-m-d H:i:s', $timestamp)
or simply
gmdate('Y-m-d H:i:s')
to get "NOW" in UTC.
Check the reference:
http://www.php.net/manual/en/function.gmdate.php
Solution 5
If you have a date in this format YYYY-MM-HH dd:mm:ss, you can actually trick php by adding a UTC at the end of your "datetime string" and use strtotime to convert it.
date_default_timezone_set('Europe/Stockholm');
print date('Y-m-d H:i:s',strtotime("2009-01-01 12:00"." UTC"))."\n";
print date('Y-m-d H:i:s',strtotime("2009-06-01 12:00"." UTC"))."\n";
This will print this:
2009-01-01 13:00:00
2009-06-01 14:00:00
And as you can see it takes care of the daylight savings time problem as well.
A little strange way to solve it.... :)
Related videos on Youtube
Jeremey
Updated on February 11, 2020Comments
-
Jeremey over 4 years
I am in need of an easy way to convert a date time stamp to UTC (from whatever timezone the server is in) HOPEFULLY without using any libraries.
-
poke over 14 yearsYou should just use
gmdate
instead of changing the default timezone to UTC. Becausegmdate
automatically uses UTC. -
Ward Bekker almost 14 years-1 because you don't want to change the global default timezone for this. It just asks for unintended side effects.
-
poke almost 12 years@cherouvim Don’t think it’s necessary, but added one anyway… Hope it helps.
-
Charlie Schliesser over 11 yearsgmdate() is one of those PHP functions I wish I had stumbled upon a long time ago. nl2br, anyone?
-
ajon almost 11 yearsTwo problems here. First you shouldn't subtract you should add the offset. Second, this doesn't work if local timezone is currently on DST and the desired date is not on DST. The offset would be off by an hour. Or Vice Versa. So yes, this works MOST of the time, but not always.
-
Jay Sheth over 10 yearsGMT and UTC are not always the same.
-
poke over 10 years@JaySheth Practically, UTC and GMT are synonymous.
-
php_nub_qq over 10 yearsI truly don't understand why the OP didn't pick your suggestion
:D
-
Nigel Alderton almost 10 years@poke To make your answer even more illuminating, can you show the output of your example code.
-
poke almost 10 years@NigelAlderton The output depends on the timezone settings of the machine running the code. But it will be something like this:
28.06.2012 23:55
. -
Matteo Tassinari almost 10 yearsTHIS is the most correct answer! Disregard che accepted one!
-
Shahid Karimi almost 9 years@Phill What is 'sz' here?
-
Shahid Karimi almost 9 yearsHell, the date string has specific time zone associated with it.
-
poke almost 9 years@Kannu Hell yes, as the question states, the parsed date string is supposed to be interpreted as the server’s local time, which is what
strtotime
does, and then converted to a date string in UTC. Remember that the Unix timestamp is by definition in UTC. So whatever timezone the server had, it is incorperated by the time you callstrtotime
. -
vaso123 over 8 years@MatteoTassinari Nope, because of the strtotime. I am working on an astrology software, and what if I want to check Leonardo Da Vinci profile? He borned in 1452. strtotime can not do anything with this. DateTime is the best for this.
-
Alex Angelico about 7 yearsI think it should be "Y-m-dTG..." instead of "Y-d-mTG..."
-
Cody Gray over 6 yearsYou should edit your answer to explain how this improves on Phil's answer. What did you change in the code, and why is it better?
-
Dharmender Tuli about 6 yearsIts not working for me in laravel project. Its return the same date
-
Necro almost 6 years@poke GMT respects daylight saving time, UTC does not and that's why it is used so you dont have to factor anything else in
-
poke almost 6 years@Necro I don’t know what exactly you are responding to there but technically, GMT is the timezone at UTC+0 without DST. BST is UTC+0 with UTC. So you could say that GMT does not respect DST but instead is only a timezone without DST.
-
Pablo Pazos over 4 yearsand the T should be 'T', the correct format is: date("Y-m-d\TH:i:sz", $the_date)
-
felwithe over 4 years@php_nub_qq probably because it works by changing the timezone for the entire script, which might not be desired and could have unintended consequences.
-
felwithe over 4 yearsThe question says convert a timestamp to a timestamp, and DateTime doesn't accept a timestamp as input. You would have to convert it to a string somehow then convert it back.
-
php_nub_qq over 4 years@felwithe It wouldn't if yoy skip the first line, which is just for demonstration
-
The Coprolal about 4 yearsChanging the default PHP timezone to convert two timestamps is a hack that works, but not an elegant solution.
-
Urasquirrel about 4 yearsSeems like over kill to convert a single datetime to UTC. What's proposed converts all datetimes nearby to UTC. With all respect this probably has caused some bugs. The person who wrote the question specified one datetime. The answer doesn't match the question. I think this deserves an edit.