PHP Pretty Date

This class is pretty much a direct port of John Resig’s JavaScript Pretty Date to PHP 5. A few notes:

  • Requires PHP >= 5.10, due to the usage of PHP’s new DateTimeclass.
  • The new DateTime object parses strings using PHP’s strtotime, so you don’t need to pass in an ISO8601 formatted date, as in JavaScript Pretty Date. Try “now”, or “next Wednesday”, or “+2 weeks 4 days 23 hours 9 seconds”.
  • Extended to handle Months and Years in the past (JavaScript version only goes to weeks)

Usage:

// pass in a String DateTime, compared to another String DateTime (defaults to now)
$myString = Date_Difference::getStringResolved('-7 weeks');
$myString = Date_Difference::getStringResolved('-7 weeks', '+1 week');
 
// pass in a DateTime object, compared to another DateTime object (defaults to now)
// useful with the Propel ORM, which uses DateTime objects internally.
$myString = Date_Difference::getString(new DateTime('-7 weeks'));
$myString = Date_Difference::getString(new DateTime('-7 weeks'), new DateTime('+1 week'));

Download PHP Pretty Date (PHP 5.10+, 1.86KB)
http://www.zachleat.com/Projects/phpPrettyDate/Date_Difference.phps

This entry was posted in PHP, Projects and tagged , . Bookmark the permalink. Both comments and trackbacks are currently closed.
  • If you found this article useful, you should subscribe to my feed (or get an e-mail). I'm also on Twitter and GitHub.
  • About the Author

    Zach Leatherman is a Professional Front End Engineer. He loves building for the web, and has been contributing to the community through his blog since February 2007. Despite his propensity for software, he has a Bachelors degree in Computer Engineering and is currently on the User Experience Team at Union Pacific Railroad. The views expressed on this website do not represent the views of his employer.

    He enjoys spending time with his beautiful wife Traci and their two Great Danes, Roxie and Ella. They also have a cat, a rabbit, goldfish, and one or more tarantulas. Read more »

6 Comments

  1. Ranie
    Posted April 23, 2009 at 6:36 pm | Permalink

    Thanks for this man! Was going to use the js version but I like the php better!

  2. Posted May 28, 2009 at 9:31 am | Permalink

    This is great, i ran into a few issues with accurate yesterday handling, but i fixed them. you can see the fix here http://noise.weareplic.com/snippets/snippet-pretty-dates-php/30/

    Loverly script.

  3. Jelle
    Posted February 18, 2010 at 5:29 am | Permalink

    Support for future!

    = 5.1 by Zach Leatherman (zachleat.com)
    // Slight modification denoted below to handle months and years.
    class prettydate
    {
    public static function getStringResolved($date, $compareTo = NULL)
    {
    if(!is_null($compareTo)) {
    $compareTo = new DateTime($compareTo);
    }
    return self::getString(new DateTime($date), $compareTo);
    }

    public static function getString(DateTime $date, DateTime $compareTo = NULL)
    {
    if(is_null($compareTo)) {
    $compareTo = new DateTime('now');
    }
    $diff = $compareTo->format('U') - $date->format('U');
    $dayDiff = floor($diff / 86400);

    if(is_nan($dayDiff)) return '';

    $end = 'ago';
    if($dayDiff < 0) { # date is in the future
    $end = 'left';
    $dayDiff *= -1;
    }

    if($dayDiff == 0) {
    if($diff < 60) {
    return 'Just now';
    } elseif($diff < 120) {
    return '1 minute '.$end;
    } elseif($diff < 3600) {
    return floor($diff/60) . ' minutes '.$end;
    } elseif($diff < 7200) {
    return '1 hour '.$end;
    } elseif($diff < 86400) {
    return floor($diff/3600) . ' hours '.$end;
    }
    } elseif($dayDiff == 1) {
    return 'Yesterday';
    } elseif($dayDiff < 7) {
    return $dayDiff . ' days '.$end;
    } elseif($dayDiff == 7) {
    return '1 week '.$end;
    } elseif($dayDiff < (7*6)) { // Modifications Start Here
    // 6 weeks at most
    return ceil($dayDiff/7) . ' weeks '.$end;
    } elseif($dayDiff < 365) {
    return ceil($dayDiff/(365/12)) . ' months '.$end;
    } else {
    $years = round($dayDiff/365);
    return $years . ' year' . ($years != 1 ? 's' : '') . ' '.$end;
    }
    }
    }

  4. Benjamin C.
    Posted May 2, 2011 at 10:17 am | Permalink

    Thanks for the script!

  5. Posted July 11, 2011 at 3:35 pm | Permalink

    Using in a new wordpress plugin, props dude, seems to fail for sub hours though…

  6. Zach Leatherman
    Posted July 11, 2011 at 5:55 pm | Permalink

    Looking back on this script, I don’t know that I’d recommend using it.

    It makes more sense to output UTC and then pretty it up using JavaScript instead. Then you can cache your HTML. See this implementation. Also on GitHub.

One Trackback

  1. By jQuery Plugin: It’s CuteTime! « The Product Guy on October 26, 2009 at 7:45 am

    [...] there are other similar tools out there in JavaScript, PHP, and, I am sure, many other languages, none adequately met my goals. Therefore, I created the [...]