Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php convert datetime to UTC

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.

like image 363
Jeremey Avatar asked Jan 19 '10 17:01

Jeremey


People also ask

How do you convert datetime to UTC?

To convert the time in a non-local time zone to UTC, use the TimeZoneInfo. ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method. If the date and time instance value is an ambiguous time, this method assumes that it is a standard time.

How to get gmt time using php?

PHP | gmdate() Function The gmdate() is an inbuilt function in PHP which is used to format a GMT/UTC date and time and return the formatted date strings. It is similar to the date() function but it returns the time in Greenwich Mean Time (GMT).

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

What is UTC time now in 24 hour format?

UTC time in ISO-8601 is 09:05:16Z. Note that the Z letter without a space.


2 Answers

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')); 
like image 62
poke Avatar answered Sep 20 '22 13:09

poke


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 
like image 34
joerx Avatar answered Sep 20 '22 13:09

joerx