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

VN:F [1.8.1_1037]
Rating: 4.8/5 (6 votes cast)
PHP Pretty Date4.856
This entry was posted in PHP, Projects and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • Thanks for reading. If you found this article useful, and you've read through some of my previous articles, you might as well Subscribe to my content subscribe to my blog. You'll save me some bandwidth that way.

2 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.

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 [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Additional comments powered by BackType