News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Davey Shafik's Blog:
DateTime Timestamp Parsing
September 20, 2011 @ 11:24:27

In a new post to his blog Davey Shafik looks at parsing dates with DateTime, the new and improved way to handle dates in PHP (well, not so new but definitely improved).

As part of a recent project, I was tasked with taking timestamps returned by an API and displaying fuzzy dates in the final output (e.g. 3hrs ago, in 2 weeks, tomorrow). The timestamp format in question looks like: 2012-09-01T16:20:01-05:00 This format can be found in PHP as the DATE_ATOM or DateTime::ATOM constants, which contain the date() formatter string: Y-m-dTH:i:sP With this in hand, we can now easily parse the timestamp into a useful object.

Parsing the date into a DateTime object is as easy as giving it the date string and telling it how it's formatted. Then you can do all sorts of fun things. He shows how to shift the timezone by name, by time increment - simple (like "1 hour") and more complex (like "1 hour 5 minutes 3 seconds"). You can find out more about the DateTime object in the PHP manual.

0 comments voice your opinion now!
datetime parsing manipulation tutorial


blog comments powered by Disqus

Similar Posts

Derick Rethans' Blog: Remote Debugging PHP with a Firewall in the Way

Michelangelo van Dam's Blog: Quality Assurance on PHP projects - PHPUnit part 2

SitePoint Web Tech Blog: How to Test Multiple Websites on One PC With Apache Virtual Hosts

Court Ewing's Blog: PHP Integration with Salesforce CRM

Gonzalo Ayuso's Blog: Pivot tables in PHP


Community Events





Don't see your event here?
Let us know!


list interview library laravel introduction series voicesoftheelephpant podcast opinion install language package framework tips release bugfix api community symfony deployment

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework