Search code examples
phptimezoneunix-timestamp

Convert unix time to UTC date/time in PHP


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.


Solution

  • gmdate("Y-m-d H:i:s", 1670716800) returns "2022-12-11 00:00:00".

    gmdate("Y-m-d H:i:s") returns the current GMT/UTC time in that format (since the GMT time is defined to be equal to the UTC time).