Publishing System Settings Logout Login Register
Strings to Time with PHP's strtotime()
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on January 17th, 2007
7825 views
PHP Coding
Very Handy Function

PHP's strtotime() function is something that if you know how to use, will prove to be one of the most handiest functions ever when working with timestamps and stuff like that.

strtotime -- Parse about any English textual datetime description into a Unix timestamp

Don't you like that?  PHP gave developers the option to type the time out, and it will provide them with a unix timestamp.  Now isn't that living large?

So lets say you are working with RSS Feeds, their time formats are really wacked up, and you usually have to use some very complex date() function in order to make them.  But the thing is, you don't usually have a way to convert it back to a unix timestamp for maximum portability.

<pubDate>Sat, 06 Jan 2007 08:00 EST</pubDate>


Thats generally how a feed timestamp looks.  Making a PHP function to do all that work for you, and to convert it to a unix timestamp would be a pain.  So just use this.

echo strtotime ( 'Sat, 06 Jan 2007 08:00 EST' );


What will you get?  But a nice unix timestamp output.  Pretty slick huh?

Let me show you how else you can use it.

Other Examples

echo strtotime("now");
echo strtotime("10 September 2000");
echo strtotime("+1 day");
echo strtotime("+1 week");
echo strtotime("+1 week 2 days 4 hours 2 seconds");
echo strtotime("next Thursday");
echo strtotime("last Monday");


Yeah, now that is PHP at its best.  Not only can you use strtotime to go in the future, but in the past as well.  It will automatically calculate that time specified according to the servers time and give you the timestamp relative to that date.  Now that comes in handy for anything really, you name it.  PHP can do it.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Chaos King

Experienced Web Developer and a Senior Developer at Pixel2Life.com. Being a role model to many future web developers, its what I do best. I enjoy long walks on the beach and arguing with Faken about life.
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top