如何将"2011-08-19 07:44:26 Etc/GMT"转换为本地时间戳?
使用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。日期和时间编程是一个相当广泛的话题。
strtotime
将字符串转换为整型时间戳,然后strftime
将整型时间戳格式化为localtime。
$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();
参见时区列表,date_default_time_zone
, DateTime::setTimezone
, DateTime::getTimestamp
的详细信息
可以使用gmmktime将GMT时间转换为UNIX时间戳:
int gmmktime ([int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]])
,然后你可以使用date来获得你的本地格式。
$timestamp = gmmktime(7,44,26,8,19,2011);
echo date("Y-m-d H:i:s",$timestamp);