Search code examples
phpdatetimetimezonegmtdate-conversion

Convert time in PHP from gmt offset


For example i have a time string of "11:00" and and gmt offset string of "+02:00".

How can i combine the two to make the conversion in PHP?

Here is my current code:

$time = $row->time;

//Get users gmt setting

$timezone = $this->_get_gmt();

//calculate correct time here

$time = ; //whatever i have to do.

All answers appreciated.


Solution

  • $date = DateTime::createFromFormat('H:i P', '11:00 +02:00');
    $date->setTimeZone(new DateTimeZone('GMT'));
    echo $date->format('H:i');
    

    Demo

    This:

    1. Creates a DateTime object with the time and offset
    2. Converts the timezone to GMT
    3. Echo's out the time in GMT