Categories
Posts

Under Appreciated Code: strtotime

There are some functions that are so useful, you sometimes wonder how anyone gets by without them. I give you the under appreciated code of the day : strtotime.

In the PHP world strtotime is a given. Here are a few examples:

[sourcecode lang=”php”]
$when = ‘yesterday’;
echo "{$when} : " . date( ‘Y-m-d H:i:s’, strtotime( $when ) ) . "n";

$when = ‘first day of last month’;
echo "{$when} : " . date( ‘Y-m-d H:i:s’, strtotime( $when ) ) . "n";

$when = ‘+45 days’;
echo "{$when} : " . date( ‘Y-m-d H:i:s’, strtotime( $when ) ) . "n";

$when = ‘+1 year 3 months 4 days 6 hours 14 seconds’;
echo "{$when} : " . date( ‘Y-m-d H:i:s’, strtotime( $when ) ) . "n";

$when = ‘last monday of next month’;
echo "{$when} : " . date( ‘Y-m-d H:i:s’, strtotime( $when ) ) . "n";
[/sourcecode]

The ability of strtotime to slice and dice dates and intervals can be a life saver. I highly recommend taking 10 minutes to try it out.

I was really disappointed to see that strtotime was not part of the Python “batteries included” approach. Fortunately there are Python implementations of strtotime, but really it is so amazingly handy that it should be up for strong consideration as a core feature.