Search code examples
phpunix-timestampdata-conversiontimestamp-with-timezone

Convert UTC timestamp to another in php


I have a timestamp in UTC timezone. Example: 1600532232. It's 4:17 PM in regular standard time.

I want to convert this timestamp to another timezone with offset and then output in regular standard time.

It should output

New york : 12:17 PM

Los Angeles : 9:17 AM

Berlin : 12:17: PM

Beijing : 11:17: PM

How do I do it in PHP ?


Solution

  • // create a new DateTime with UTC tz
    $epoch = 1600532232;
    $dt = new DateTime("@$epoch", new DateTimeZone('UTC'));
    
    // change the timezone
    $dt->setTimezone(new DateTimeZone('America/New_York'));
    
    // format the date
    $dt->format('h:i A');