开发者

php strtotime not working

so I converted this string to timestamp using strtotime:

strtotime("1 Nov, 2001");

which results into the timestamp 1320177660

but then when I tried converted 1320177660 into a normal date format a开发者_高级运维gain using an online timestamp converter, the year ended up being 2011 rather than 2001...

what am I doing wrong?


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";
?>

Output:

1004590800
November 01, 2001, 00:00:00


You're not doing anything wrong - it's just that strtotime isn't infallible.

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.


If strtotime() is not working then try this

strtotime(str_replace('/','', $date ));

It will work properly. Don't use '/' in date, because when you will format this then it may detect this type of issue. Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜