Parsing human readable times

So what day was 9 days ago again?

$ when 9 days ago

2014-07-13

How do we do it?

Through the miracle of PHP1!

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

date_default_timezone_set('America/Los_Angeles');
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

2014-07-17

$ when 2 months ago

2014-05-22

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 [return]
comments powered by Disqus