0

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.

Tapha
  • 1,491
  • 3
  • 17
  • 32

3 Answers3

2
$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
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

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";
Machavity
  • 30,841
  • 27
  • 92
  • 100
1

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.

Machavity
  • 30,841
  • 27
  • 92
  • 100