How to convert to UTC

I get dates and times from different sources (for example, file date / time from an FTP server, email date / time, etc.), and you need to store them in UTC format (so everyone has a common link). How can I do it? What parts of the information do I need for the correct conversion.

This is for a PHP web application. So, I can get the time zone of the server. I'm not sure what to do next. Here are some input examples:

  • Mon, 28 Jun 2010 12:39:52 +1200
  • 2010-06-25 15:33:00
+4
source share
2 answers

In the first case, the offset exists, so it should be trivial, the second example, however, will be considered as UTC (or any other default time zone). Here is what I suggest:

date_default_timezone_set('UTC'); // set default timezone $one = strtotime('Mon, 28 Jun 2010 12:39:52 +1200'); $two = strtotime('2010-06-25 15:33:00'); // Already UTC? Must be... 

$one and $two will hold timestamps corresponding to the time converted to the UTC time zone.

+3
source

You can use strtotime() to convert any time format you have into a timestamp, and then use any function, possibly date() , put it in the format in which you want to save everything.

+3
source

Source: https://habr.com/ru/post/1315601/


All Articles