我有以下日期格式
$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 - 如何在php中将不同时区的日期和时间转换为GMT时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23993362/