简体   繁体   English

php strtotime无法正常工作

[英]php strtotime not working

so I converted this string to timestamp using strtotime: 所以我使用strtotime将此字符串转换为时间戳:

strtotime("1 Nov, 2001");

which results into the timestamp 1320177660 这导致时间戳1320177660

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... 但是当我尝试使用在线时间戳转换器再次将1320177660转换为正常日期格式时,年份最终是2011年而不是2001年......

what am I doing wrong? 我究竟做错了什么?

As @Evan Mulawski's comment says, the "2001" is being interpreted as the time, not the year. 正如@Evan Mulawski的评论所说,“2001”被解释为时间,而不是一年。 Take out the comma to get PHP to interpret the "2001" as a year: 取出逗号让PHP将“2001”解释为一年:

<?php
$ts = strtotime("1 Nov 2001");
echo $ts . "\n";
$st = strftime("%B %d, %Y, %H:%M:%S", $ts);
echo $st . "\n";
?>

Output: 输出:

1004590800 1004590800
November 01, 2001, 00:00:00 2001年11月1日00:00:00

You're not doing anything wrong - it's just that strtotime isn't infallible. 你没有做错任何事 - 只是strtotime不是绝对正确的。

As such, if you know the string is always going to be in a certain format, it's sometimes advisable to parse the date yourself, or if you're running PHP 5.3.x, you could use DateTime::createFromFormat to parse the string for you. 因此,如果您知道字符串总是采用某种格式,有时建议您自己解析日期,或者如果您运行的是PHP 5.3.x,则可以使用DateTime :: createFromFormat来解析字符串您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM