Categories
Posts

Under Appreciated Code: strtotime

There are some functions that are so useful, you sometimes wonder how anyone gets by without them. I give you the under appreciated code of the day : strtotime.

In the PHP world strtotime is a given. Here are a few examples:

[sourcecode lang=”php”]
$when = ‘yesterday’;
echo "{$when} : " . date( ‘Y-m-d H:i:s’, strtotime( $when ) ) . "n";

$when = ‘first day of last month’;
echo "{$when} : " . date( ‘Y-m-d H:i:s’, strtotime( $when ) ) . "n";

$when = ‘+45 days’;
echo "{$when} : " . date( ‘Y-m-d H:i:s’, strtotime( $when ) ) . "n";

$when = ‘+1 year 3 months 4 days 6 hours 14 seconds’;
echo "{$when} : " . date( ‘Y-m-d H:i:s’, strtotime( $when ) ) . "n";

$when = ‘last monday of next month’;
echo "{$when} : " . date( ‘Y-m-d H:i:s’, strtotime( $when ) ) . "n";
[/sourcecode]

The ability of strtotime to slice and dice dates and intervals can be a life saver. I highly recommend taking 10 minutes to try it out.

I was really disappointed to see that strtotime was not part of the Python “batteries included” approach. Fortunately there are Python implementations of strtotime, but really it is so amazingly handy that it should be up for strong consideration as a core feature.

11 replies on “Under Appreciated Code: strtotime”

As far as simplicity, this use for this function is really cool and may be a good alternative for someone who doesn’t understand the equivalent computer logic.

As a developer, I do not like this function used in this way since logic and the english language do not mix well. To truly be bullet proof, the use of mktime() will solve the above examples with simple arithmetic. The problem with a function like this is expectations, what if someone wanted ‘first day of last month excluding March and September’.

Personally, I would be pretty upset if this kind of code was found in a banking web site or in a defibrillator. Otherwise, cool stuff!

I’m not sure I understand the your problem with strtotime(). If it works, use it. If it doesn’t, do something else. What I’ve found is that it works for many cases, making it very handy.

i like the function, I like to have as many tools/options as possible in my toolbox. I just would not trust such a solution for anything that needs to be absolutely 100%.

Assumptions are the biggest bugs in programming. Assuming a function that translates english to logic will always be correct is a poor assumption. Good enough for a vanilla web site, but not good enough when your money or your life is on the line.

I don’t think I ever suggested that strtotime() was right absolutely 100% of the time. I hate to quote myself, but “If it works, use it. If it doesnโ€™t, do something else”. Obviously the more critical the code path, the more testing that needs to be done to make sure it does actually work for various cases.

You may want to read up on some of the other formats that strtotime() supports, it isn’t just English style wording. http://php.net/manual/en/function.strtotime.php

If it doesn’t work for you, don’t use it. Not a big deal ๐Ÿ™‚

No worries here, I’m just giving my opinion. I too like the function, I also like get_headers, but these functions have their problems too.

I used to use strtotime all the time for converting readable dates in any non-critical situations. Sometime in 2002 strtotime inverted a date on me, I believe it was a date in the International format YY/MM/DD that the strtotime function translated to the US format MM/DD/YY. I use preg_match to parse my dates in these situations, I mainly rely on strtotime for parsing RFC defined dates and even then if the application was for banking or for life/death use I wouldn’t even touch strtotime.

You may want to read up on some of the other problems with strtotime(), it isnโ€™t just translating English to logic. http://php.net/manual/en/function.strtotime.php ๐Ÿ™‚

haha, running out of seconds since 1 Jan 1970 is going be a major let done. Sometime around Tue Jan 19 03:14:07 2038 we will all really be wishing they’d used an unsigned 32bit int instead of a signed one.

Of course everyone will have updated to 64bit time storage by then ๐Ÿ˜‰

Leave a Reply

Your email address will not be published. Required fields are marked *