在PHP中将Etc / GMT日期转换为本地

时间:2011-08-19 07:46:42

标签: php time

如何将“2011-08-19 07:44:26 Etc / GMT”转换为本地时间戳?

4 个答案:

答案 0 :(得分:1)

使用DateTime::createFromFormat

$d = DateTime::createFromFormat("Y-m-d H:i:s T", "2011-08-19 07:44:26 Etc/GMT");
echo $d->format("r"), "\n";
$d->setTimezone(new DateTimeZone("EST"));
echo $d->format("r"), "\n";

将输出

Fri, 19 Aug 2011 07:44:26 +0000
Fri, 19 Aug 2011 03:44:26 -0400

有一本关于这个主题的好书:php|architect's Guide to Date and Time Programming。使用日期和时间编程是一个非常广泛的主题。

答案 1 :(得分:0)

strtotime会将字符串转换为整数时间戳,然后strftime会将整数时间戳格式化为本地时间。

答案 2 :(得分:0)

$UTC_timezone = new DateTimeZone("GMT");
$date = new DateTime("2011-08-19 07:44:26",$UTC_timezone);
echo $date->format("Y-m-d H:i:s");echo "<br/>";
$current_timezone = new DateTimeZone(date_default_timezone_get());
$date->setTimezone($current_timezone);
echo $date->format("Y-m-d H:i:s");

$timestamp = $date->getTimestamp();

有关详情,请参阅list of timezonesdate_default_time_zoneDateTime::setTimezoneDateTime::getTimestamp

答案 3 :(得分:0)

您可以使用gmmktime将GMT时间转换为UNIX时间戳:

int gmmktime([int hour [,int minute [,int second [,int month [,int day [,int year [,int is_dst]]]]]]])

然后您可以使用日期来获取本地格式。

$timestamp = gmmktime(7,44,26,8,19,2011);
echo date("Y-m-d H:i:s",$timestamp);
相关问题