PHP的strtotime无法正常工作
问题描述:
所以我使用strtotime将此字符串转换为时间戳:
so I converted this string to timestamp using strtotime:
strtotime("1 Nov, 2001");
这会导致时间戳记1320177660
which results into the timestamp 1320177660
但是当我尝试使用在线时间戳转换器再次将1320177660转换为正常日期格式时,年份最终是2011年而不是2001年...
but then when I tried converted 1320177660 into a normal date format again using an online timestamp converter, the year ended up being 2011 rather than 2001...
我在做什么错?
答
正如@Evan Mulawski的评论所说,"2001"被解释为时间,而不是年份.取出逗号让PHP将"2001"解释为一年:
As @Evan Mulawski's comment says, the "2001" is being interpreted as the time, not the year. Take out the comma to get PHP to interpret the "2001" as a year:
<?php
$ts = strtotime("1 Nov 2001");
echo $ts . "\n";
$st = strftime("%B %d, %Y, %H:%M:%S", $ts);
echo $st . "\n";
?>
输出:
1004590800
2001年11月1日,00:00:00
1004590800
November 01, 2001, 00:00:00