Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does strtotime('a') return a time?

Tags:

php

I am looping through a CSV file in PHP 5.3 and checking for dates. I have been using strtotime(), and it has worked well, except for one field I have that contains either a 1 or 2 char code.

strtotime() on any single char code seems to act like I am asking for now(), but if the code is 2 chars, it fails, as I would expect it to.

What am I not understanding about the way strtotime() works?

like image 329
ursasmar Avatar asked Feb 22 '23 14:02

ursasmar


2 Answers

Take a look at http://www.php.net/manual/en/datetime.formats.time.php time formats. It seems that 'a' is a timezone. It might just be a mistake that 'a' works and it's resolving to something else, but at least that's the explanation.

like image 178
Explosion Pills Avatar answered Feb 25 '23 02:02

Explosion Pills


strtotime basically does it best to interpret what it your text means, I don't think you can rely on it failing on certain strings.

Example:

<?php

echo 'Now: ' . date('c') . PHP_EOL;
echo PHP_EOL;
for($c = 'A'; $c <= 'z'; $c = chr(ord($c) + 1)) {
    echo $c . ' - ' . date('c', strtotime($c)) . PHP_EOL;
}

If I put the output next to http://www.timeanddate.com/library/abbreviations/timezones/military/ , I can only support @tandu 's conclusion that it interprets it as timezones (including the J letter that results in an invalid date).

It's a best effort, I guess. If you have a problem with the way it deals with single letters, add a check up front. Not really nice, but that's what you get when you try to parse irregular data.

like image 21
Johan B.W. de Vries Avatar answered Feb 25 '23 02:02

Johan B.W. de Vries