Zach’s ugly mug (his face)

Zach Leatherman

PHP Pretty Date

February 10, 2008

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 Web Craftsperson with the award winning Filament Group. He’s currently fixated on web fonts and static site generators. His public speaking résumé includes talks in eight different countries at events like Beyond Tellerrand, Smashing Conference, CSSConf, and The White House. He also helps herd NEJS CONF and the NebraskaJS meetup. Read more about Zach »

6 Replies
    1. Thanks for this man! Was going to use the js version but I like the php better!
      1. 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.
        1. 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; } } }
          1. Benjamin C. Disqus

            02 May 2011 at 03:17PM
            Thanks for the script!
            1. Using in a new wordpress plugin, props dude, seems to fail for sub hours though...
              1. Zach Leatherman Disqus

                11 Jul 2011 at 10:55PM
                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.
                Webmentions