Tuesday, December 30, 2008

PHP's strtotime

I generally think of PHP as the buck-toothed high-school drop-out sibling of Perl (itself the unbathed cousin of Python and Ruby). But today I discovered the library function strtotime, which is a very flexible parser for text expressing a date/time. One cool example is a snippet that calculates the current year's Thanksgiving day.

The closest Python equivalent to strtotime seems to be dateutil.parser.parse, though it's much less flexible from a cursory glance. Of course, if a person is only trying to compute "common moveable Christian feasts that can be deduced from the date of Easter Sunday" they need look no further than the mx.DateTime.Feasts library (which includes translations into German and French). Weird. And awesome.

2 comments:

Erik Peterson said...

Ruby's Date.parse is just as good, from what I've seen:

Date.parse("Tuesday, December 30th, 2008").to_s
=> "2008-12-30"


I can't figure out how to do the Thanksgiving thing, though.

joeatwork said...

Gotta say, I resent your characterization of Perl as an "unwashed cousin" when you've easy access to "crazy uncle, played by Christopher Lloyd". Sure, its socks don't match, and it does smell a little funny, but you should see what happens when it gets to 88mph :)