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
6 Replies
Ranie Disqus
23 Apr 2009 at 11:36PMDevan Disqus
28 May 2009 at 02:31PMJelle Disqus
18 Feb 2010 at 10:29AMBenjamin C. Disqus
02 May 2011 at 03:17PMWoody Disqus
11 Jul 2011 at 08:35PMZach Leatherman Disqus
11 Jul 2011 at 10:55PM