kamikaze_pilot
kamikaze_pilot

Reputation: 14834

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 again using an online timestamp converter, the year ended up being 2011 rather than 2001...

what am I doing wrong?

Upvotes: 4

Views: 4792

Answers (3)

Web Developer Kanai
Web Developer Kanai

Reputation: 29

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

Upvotes: 0

GreenMatt
GreenMatt

Reputation: 18570

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

Upvotes: 5

John Parker
John Parker

Reputation: 54415

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.

Upvotes: 4

Related Questions