如何转换不同的时区日期&时间到格林威治标准时间在PHP

时间:2014-06-02 11:23:20

标签: php date

我有以下日期格式

$date_format1 = "Sat, 31 May 2014 00:00:00 +0200";
$date_format2 = "Mon, 02 Jun 2014 14:00:00 -0400";
$date_format3 = "Mon, 02 Jun 2014 11:03:00 BST";
$date_format4 = "Mon, 02 Jun 2014 10:03:00 EDT";

在GMT上转换所有上述格式的PHP代码是什么? 注意:我只需要单个(如果可能)功能来处理上述所有格式。

5 个答案:

答案 0 :(得分:1)

请阅读有关php函数gmdate()

的信息

答案 1 :(得分:1)

gmdate()函数将以GMT格式输出日期/时间,strtotime()函数会将日期时间字符串转换为gmdate()的有效参数。

$date_format1 = "Sat, 31 May 2014 00:00:00 +0200";
$date_format2 = "Mon, 02 Jun 2014 14:00:00 -0400";
$date_format3 = "Mon, 02 Jun 2014 11:03:00 BST";
$date_format4 = "Mon, 02 Jun 2014 10:03:00 EDT";


$newFormat = 'd/m/Y H:i:s';


echo gmdate($newFormat, strtotime($date_format1)) . PHP_EOL;
echo gmdate($newFormat, strtotime($date_format2)) . PHP_EOL;
echo gmdate($newFormat, strtotime($date_format3)) . PHP_EOL;
echo gmdate($newFormat, strtotime($date_format4)) . PHP_EOL;

结果将是: -

30/05/2014 22:00:00
02/06/2014 18:00:00
02/06/2014 10:03:00
02/06/2014 14:03:00

答案 2 :(得分:0)

这准确地给出了GMT时间:

gmdate('Y-m-d H:i:s', strtotime ('+1 hour')) 

答案 3 :(得分:0)

PHP函数strtotime (string $time)可用于将$date_format1的文本部分转换为可在gmstrftime()函数中使用的时间戳,因此:

gmdate("<insert_date_format_here>", strtotime($date_format1));

请参阅PHP文档中的Date FormatsTime Formats

答案 4 :(得分:0)

我为你创造了这个功能,它很容易帮助你

function datetoutc($d , $zone="UTC"){
    $d = new DateTime($d);
    $d->setTimezone(new DateTimeZone($zone));
    return $d->getTimestamp();
}

print datetoutc("Sat, 31 May 2014 23:00:00 +0200");
相关问题