Search code examples
phpdatetimetimezone-offsetgmttimestamp-with-timezone

How to calculate timestamp from given timestamp and GMTOffset value in PHP?


Currently I am using an API for flights search that shows flight details. In API response, few dates and GMTOffset is returned. I need to convert these to Pakistan Standard Time. API Response looks as following:

DepartureDateTime: 2018-11-10T14:20:00

ArrivalDateTime: 2018-11-11T07:10:00

DepartureTimeZone => "GMTOffset": -5

ArrivalTimeZone => "GMTOffset": 3

Kindly guide me how can I convert above date/time to Pakistan Standard Date/Time because I do not have an idea how these GMTOffset values will help me to get what I want.

Thank you!


Solution

  • You can use DateTime and DateTimeZone to set the UTC and your local timezone, this will automatically convert UTC to your local time:

    $utc = "2018-11-10T14:20:00";
    $dt = new DateTime($utc);
    $tz = new DateTimeZone('Asia/Karachi'); // or whatever zone you're after
    
    $dt->setTimezone($tz);
    echo $dt->format('d-m-Y H:i:s');
    

    Output: 10-11-2018 19:20:00