Tapha
Tapha

Reputation: 1511

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.

Upvotes: 0

Views: 988

Answers (3)

John Conde
John Conde

Reputation: 219834

$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

Upvotes: 2

Try:

$time = $row->time;

$timezone = $this->_get_gmt();

$time = date( "H:i:s", strtotime( $time )+ $timezone * 60 * 60 )

assuming that $time is in time format: eg. 14:30, and $timezone is offset number eg. 2.

Upvotes: 1

Machavity
Machavity

Reputation: 31654

You can use a DateTime object to do that

$date = new DateTime('Your Time String here');
$date->setTimezone(new DateTimeZone('GMT'));
echo $date->format('Y-m-d H:i:sP') . "\n";

Upvotes: 1

Related Questions