Skip to content

Instantly share code, notes, and snippets.

@jak
Created April 22, 2012 19:57
Show Gist options
  • Select an option

  • Save jak/2466500 to your computer and use it in GitHub Desktop.

Select an option

Save jak/2466500 to your computer and use it in GitHub Desktop.

Revisions

  1. Jak Spalding created this gist Apr 22, 2012.
    39 changes: 39 additions & 0 deletions gistfile1.aw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    <?php

    function pluralise($count, $single, $plural = null) {
    if ($count == 1 || $count == -1) {
    return $single;
    }
    if ($plural == null) {
    $plural = $single . 's';
    }
    return $plural;
    }

    function from_time($time) {
    $now = new DateTime('now');
    $then = new DateTime($time);
    $timespan = $now->diff($then);
    $message = array();
    if ($timespan->y)
    $message[] = $timespan->y.' '.pluralise($timespan->y, 'year');
    if ($timespan->m)
    $message[] = $timespan->m.' '.pluralise($timespan->m, 'month');
    if ($timespan->d)
    $message[] = $timespan->d.' '.pluralise($timespan->d, 'day');
    if ($timespan->h)
    $message[] = $timespan->h.' '.pluralise($timespan->h, 'hour');
    if ($timespan->i)
    $message[] = $timespan->i.' '.pluralise($timespan->i, 'minute');
    if ($timespan->s)
    $message[] = $timespan->s.' '.pluralise($timespan->s, 'second');
    if (count($message) == 0) {
    return 'just now';
    }
    $output = array_shift($message);
    if ($message) {
    $output .= ' and '.array_shift($message);
    }
    $output .= ' ago';
    return $output;
    }