Ronald Fernandes
Ronald Fernandes

Reputation: 285

Show GMT Offset in PHP

I have a timezone database from https://timezonedb.com/download

Each Time Zone row queried has one time_start field and another field called gmt_offset

For example one row would be

        [zone_id] => 191
        [zone_name] => Asia/Kolkata
        [country_name] => India
        [time_start] => -891582800
        [gmt_offset] => 23400
        [dst] => 0

What I need to display from this is the Country and the GMT Offset like this via PHP

India: GMT +5:30

I've tried

            $plusoffset = $row->time_start+$row->gmt_offset;
            $minusoffset = $row->time_start-$row->gmt_offset;
            echo "Time Start: ". date('h:i:s',$row->time_start).'<br>';
            echo "Offset: ". date('h:i:s',$row->gmt_offset).'<br>';
            echo "Start+Offset: ". date('h:i:s',$plusoffset).'<br>';
            echo "Start-Offset: ". date('h:i:s',$minusoffset).'<br>';

But none of these show the correct offset.

I'm sure I'm missing something glaringly obvious but I can't for the life of me figure it out.

Upvotes: 1

Views: 194

Answers (1)

David Constantine
David Constantine

Reputation: 587

Just translate the seconds you got there (23400) to hours and minutes...

$gmt_offset = 23400;

$hours = (int)($gmt_offset / 3600);
$minutes = $gmt_offset % 3600 / 60;
echo 'Offset: ' . $hours . ':' . $minutes;

Upvotes: 1

Related Questions