Php strtotime not working

so I converted this string to timestamp using strtotime:

strtotime("1 Nov, 2001"); 

which leads to timestamp 1320177660

but then when I tried to convert 1320177660 to a regular date format again using the online timestamp converter, the year ended with 2011, not 2001 ...

what am I doing wrong?

+4
source share
2 answers

As @Evan Mulawski notes, "2001" is interpreted as time, not year. Take out the comma so that PHP interprets "2001" as the 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

+5
source

You are not doing anything wrong - just strtotime is not infallible.

Thus, if you know that the string will always be in a specific format, it is sometimes recommended to analyze the date yourself, or if you are using PHP 5.3.x, you can use DateTime :: createFromFormat to parse the string for you.

+4
source

All Articles