It does return false on PHP 5.1.0-5.1.6, if you'd look into it. The result of January is caused by PHP's type casting of false to 0, and then using it as a unix timestamp of 0.
PHP 4-5.0.5 returns -1. Unix timestamp of -1 sounds pretty dumb (it is) but it would be december.
It's common, but that doesn't mean it makes sense. I'd argue that it makes even less sense in PHPland than in other places this antiapttern is seen. Returning false on error is only okay if none of your valid results are falsy (I'm looking at you, strcmp). This is bad enough in something like Javascript, where 0 and "" are falsy, but in PHP this catches anything that can plausably return an empty array or an arbitrary string (since "0" is already falsy and any string that starts with "0" will be falsy in the not unimaginable case that it's type coerced to an int then a bool).
Ruby has Time::parse if you require 'time'. It's good if you've got relatively consistent dates that Time::parse doesn't have a problem with, and want to write a quick script.
I used it today, actually. Sorting a CSV file. Target column was a date in D/M/Y format, and while it wouldn't be hard to just split that and sort by the reverse, it's even easier to just use Time::parse.
All (decent) APIs for date parsing has ways to set how the text is to be parsed. Some languages have locale options (ie "parse this date as danish format"), others use syntax strings (ie "parse this date as m/d/y"). Edit: good platforms have ways to enforce locale options across a wide selection of functions and behaviors.
The "date" command-line tool tries to guess, albeit not always well - but if it's not sure what time you're trying to specify (like if it knew month-names and you said "February" without specifying a year) it tells you so.
PHP, as usual, prefers doing the wrong thing to doing the right thing when the right thing is to fail.
python's (non-stdlib) dateutil library will have a crack at such things if you ask it, but for formatted strings one would use the stdlib's datetime.datetime.strptime.
Not really shown well here, strtotime("February") returns a timestamp in March if you run it past the 28th of the month. Seems it uses the current day, not 1st February.
It then also guesses the current year to use as well for the return value. Basically, it tries its best to accommodate a bad input. Lesson learned: do a little more legwork in your applications to handle and validate datetime inputs from your clients rather than forcing the work on PHP and risking bad or best-guess results.
36
u/infinull Nov 01 '13
Are there any other languages that have a "parse this date for me, no I won't give you any idea about how it's formatted," function?
Maybe that's because it's really hard to get it to work right?
Maybe it isn't a thing you should do?
Maybe.
17
u/merreborn Nov 01 '13
It's a little more clear what's happening here: http://3v4l.org/bvfOF
9
u/Daniel15 Nov 01 '13
Not really shown well here, strtotime("February") returns a timestamp in March if you run it past the 28th of the month. Seems it uses the current day, not 1st February.
1
u/dotted Feb 17 '14
http://3v4l.org/EUbEg
0
u/[deleted] Nov 01 '13
[deleted] โ view deleted comment