我有以下日期格式
$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代码是什么?注意:我只需要一个(如果可能的话)函数来处理上述所有格式。
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
请阅读php函数gmdate()
PHP函数strtotime (string $time)
可用于将$date_format1
的文本部分转换为可以在gmstrftime()
函数中使用的时间戳,因此:
gmdate("<insert_date_format_here>", strtotime($date_format1));
请参考PHP文档中的日期格式和时间格式
这就是GMT时间:
gmdate('Y-m-d H:i:s', strtotime ('+1 hour'))
我为你创建了这个函数,它很容易,希望它能帮助你
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");