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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With