简体   繁体   English

从gmt偏移量转换PHP时间

[英]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". 例如,我的时间字符串为“ 11:00”,而gmt偏移字符串为“ +02:00”。

How can i combine the two to make the conversion in PHP? 如何将两者结合起来进行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. 所有答案表示赞赏。

$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 创建一个具有时间和偏移量的DateTime对象
  2. Converts the timezone to GMT 将时区转换为GMT
  3. Echo's out the time in GMT 回声在格林尼治标准时间

You can use a DateTime object to do that 您可以使用DateTime对象执行此操作

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

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. 假设$time为时间格式:例如 14:30, and $timezone is offset number eg. 14:30, $timezone是偏移量,例如。 2. 2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM