Zach’s ugly mug (his face)

Zach Leatherman

PHP Pretty Date

February 10, 2008

Warning

This article is old and may contain information that is outdated, irrelevant, or—dare I say it—no longer accurate. Read with care!

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

Zach’s ugly mug (his face)

Zach is a builder for the web with IndieWeb Avatar for https://www.netlify.comNetlify. He created the IndieWeb Avatar for https://www.11ty.devEleventy site generator and is still fixated on web fonts. His public speaking résumé includes talks in eight different countries at events like Jamstack Conf, Beyond Tellerrand, Smashing Conference, CSSConf, and The White House. He is an emeritus of Filament Group, NEJS CONF, and still helps out with nebraskajs’s AvatarNebraskaJS. Read more about Zach »

Previous
Specific Inheritance with TIBCO’s General Interface
Next
Online Movie Rental Review – Amazon Unbox
6 Comments
  1. Ranie Disqus

    24 Apr 2009
    Thanks for this man! Was going to use the js version but I like the php better!
  2. Devan Disqus

    28 May 2009
    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/...Loverly script.
  3. Jelle Disqus

    18 Feb 2010
    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 = &#039left&#039;$dayDiff *= -1;} if($dayDiff == 0) { if($diff < 60) { return &#039Just now&#039; } elseif($diff < 120) { return &#0391 minute &#039.$end; } elseif($diff < 3600) { return floor($diff/60) . &#039 minutes &#039.$end; } elseif($diff < 7200) { return &#0391 hour &#039.$end; } elseif($diff < 86400) { return floor($diff/3600) . &#039 hours &#039.$end; } } elseif($dayDiff == 1) { return &#039Yesterday&#039; } elseif($dayDiff < 7) { return $dayDiff . &#039 days &#039.$end; } elseif($dayDiff == 7) { return &#0391 week &#039.$end; } elseif($dayDiff < (7*6)) { // Modifications Start Here // 6 weeks at most return ceil($dayDiff/7) . &#039 weeks &#039.$end; } elseif($dayDiff < 365) { return ceil($dayDiff/(365/12)) . &#039 months &#039.$end; } else { $years = round($dayDiff/365); return $years . &#039 year&#039 . ($years != 1 ? &#039s&#039 : &#039&#039) . &#039 &#039.$end; } } }
  4. Benjamin C. Disqus

    02 May 2011
    Thanks for the script!
  5. Woody Disqus

    12 Jul 2011
    Using in a new wordpress plugin, props dude, seems to fail for sub hours though...
  6. Zach Leatherman Disqus

    12 Jul 2011
    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.

Shamelessly plug your related post:

Sharing on social media?

This is what will show up when you share this post on Social Media:

How did you do this? I automated my Open Graph images. (Peer behind the curtain at the test page)