Its called “strtotime()”, do you use it?

PHP+MySQL, Tips+Tricks Add comments
php - PHP: Hypertext Preprocessor

I am a user note maintainer on the php Documentation team. I spend a lot of time everyday reading new notes and comments that have been added to the documentation. It is us maintainers that decide whether a user note stays in the documentation or needs to be removed. We simply just have to ask ourselves: “Does this note or comment truly bring a good amount of benefit to the reader, or is it just taking up space and/or repeating what has previously been said?”.

There are definitely some gems. A lot of passionate people out there come up with some excellent ideas, examples, and suggestions. If that was all there was, we would not be needed to help maintain the user notes. Unfortunately, the majority of the notes fall more into the garbage category.

I wanted to take this opportunity to talk about a certain php function that just does not get used in situations it ought to be used in. I am talking about strtotime(). I can not tell you home many times I have had to delete a note because it consisted of 10+ lines of code that in the end, determined what day was last Monday. With strtotime(), its as easy as writing strtotime(”Last Monday”) - and than bam, you are returned the Unix time stamp of last Monday.

Is strtotime() limited to just this use? - NO! They have packed in so many amazing string variables into this function that its ease of use is amazing. If you have a badly formatted date string that strtotime() does not understand, its easy enough to correct that date string and put it in a readable format with other php functions and still in the end, you have coded so much less than you would have if you were using some of the functions I have seen.

I guess I would understand if this function was a php5 only function, since it seems so many people are out there and not up to date with the latest php. But that is not the case, strtotime has been available since php4! This simple little function can be so useful. I highly suggest that if you code php and deal with a lot of awkward formed date strings, you need to look into this function.

Did you like this post? I write many like it all the time!
Please Subscribe To My RSS Feed!
To catch many more articles like this in the future, make it easy on yourself and subscribe to me via RSS. You will not regret it!

Add This Site To Your Technorati Favorites!
Let me know that you added me by posting a comment below and then I will add you as a favorite of mine as well!

Please Give Me Some Rice!
Giving me "rice" on Nice4Rice.com also benefits you by giving you a free linkback on that site too.

RSS feed | Trackback URI

7 Comments »

Comment by Saman Sadeghi UNITED STATES Windows XP Mozilla Firefox 2.0.0.2
2007-03-29 10:34:31

PHP is so powerful - it’s really amazing! Are you going to be writing more PHP articles?

 
Comment by RJ Matthis UNITED STATES Windows XP Mozilla Firefox 2.0.0.3
2007-03-29 12:09:34

I hope to. I’m going to work out the brain muscle and see if I have any talent at technical articles.

Its hard though, a lot of people that come to php want the quick answers - strtotime() is a quick answer, but a lot of functions do take practice of teaming many together to make happen all the wonderful things it is capable of happen.

 
Comment by Saman Sadeghi UNITED STATES Windows XP Mozilla Firefox 2.0.0.3
2007-03-30 15:57:58

Very true, a lot of visitors want the quick and easy answer - especially with a specialized topic like PHP but, if you build a reputation for a site that has the answers, they will keep coming back!

 
Comment by Scot Smith UNITED STATES Windows XP Mozilla Firefox 2.0.0.3
2007-04-02 03:51:39

Interesting tip– I can follow what you’re saying about people going the unorthodox route.

 
Comment by DanyO UNITED STATES Windows XP Internet Explorer 6.0
2007-04-10 04:25:47

Very interresting function. I’ll be sure to use it next time I need to convert or get times.

 
Comment by Spud Oregon JAPAN Windows Vista Internet Explorer 7.0
2007-04-21 06:49:03

I use strtotime. It’s great! I’m pretty new to php though I do have a programming background. I was reluctant to switch from Perl to php, but I’m glad I did!

 
2007-04-27 22:50:11

[…] Lerdorf To your expanding branches fed by Coder’s hard work Oh why do people refuse to use strtotime()? The correct function for the job makes your app […]

 

Leave a Reply

Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.
"Reformatted", but based on a WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login
Close
E-mail It