我收到以下格式2015-01-09T20:46:00+0100
的日期,需要按时间戳转换。
不幸的是,strtotime函数忽略了时区组件:
print strtotime('2015-01-09T20:46:00+0100') . "\n";
print strtotime('2015-01-09T20:46:00');
//result is the same with or without timezone:
//1420832760
//1420832760
解决此问题的正确方法是什么?
答案 0 :(得分:1)
DateTime正确处理:
$date = new DateTime('2015-01-09T20:46:00+0100');
echo $date->getTimestamp();
echo "\n";
$date = new DateTime('2015-01-09T20:46:00');
echo $date->getTimestamp();
1420832760
1420836360
答案 1 :(得分:0)
我老了!
使用默认时区,除非在其中指定了时区 参数 http://php.net/manual/en/function.strtotime.php
这就是结果与时区相同的原因:
date_default_timezone_set('Europe/Paris');
print strtotime('2015-01-09T20:46:00+0200');
print "\n";
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');
print "\n\n";
date_default_timezone_set('UTC');
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');
输出:
1420829160
1420832760
1420832760
1420832760
1420836360
感谢您的帮助!