Home:ALL Converter>Convert unix time to UTC date/time in PHP

Convert unix time to UTC date/time in PHP

Ask Time:2022-12-11T23:23:23         Author:Kolodez

Json Formatter

The UTC day 2022-12-11 starts at unix time 1670716800 (which is obviously divisible by 24*3600).

In PHP, I can convert it from left to right this way:

$Date = "2022-12-11";
echo strtotime ($Date." 00:00 UTC"); 

As expected, the output is 1670716800.

But how to convert it back? If I do getdate(1670716800), I get a date with a time, but in my local time zone, whereas I want UTC.

I do not want to use object oriented PHP if this can be avoided.

Author:Kolodez,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74762038/convert-unix-time-to-utc-date-time-in-php
RiggsFolly :

My timezone is UTC so you will get different results, but use the date_default_timezone_set() function to dafine what the date() function will do with the timestamp\n$Date = "2022-12-11";\n$tstamp = strtotime ($Date." 00:00:00 UTC");\n\necho date_default_timezone_get() . '-';\necho date('Y/m/d H:i:s', $tstamp) . PHP_EOL. PHP_EOL;\n\n\ndate_default_timezone_set('America/Los_Angeles');\necho date_default_timezone_get() . '-';\necho date('Y/m/d H:i:s', $tstamp) . PHP_EOL. PHP_EOL;\n\ndate_default_timezone_set('Australia/Brisbane');\necho date_default_timezone_get() . '-';\necho date('Y/m/d H:i:s', $tstamp) . PHP_EOL;\n\nRESULTs\nUTC-2022/12/11 00:00:00\n\nAmerica/Los_Angeles-2022/12/10 16:00:00\n\nAustralia/Brisbane-2022/12/11 10:00:00\n",
2022-12-11T19:20:45
yy