Parsing human readable times

So what day was 9 days ago again?

$ when 9 days ago


How do we do it?

Through the miracle of PHP1!

#!/usr/bin/env php
$date_string = implode(' ', $argv);

echo date('Y-m-d', strtotime($date_string)) . "\n";

One thing that PHP has going for it is a really nice date parsing function strtotime. Give it just about anything and it will turn it into a date.

array_shift is used to pop off the first argument, which is the name of the function. implode will stick the rest of the arguments together as a string rather than an array.

$ when last thursday


$ when 2 months ago


Particularly useful for the new style github interface. It’s nice to have human readable dates in commit logs, but not always what I want.

The entire source code is in my dotfiles repository: when.

  1. A phrase I never expected to utter ↩︎