tags:

views:

1074

answers:

4

I really like the PHP function strtotime(), but the user manual doesn't give a complete description of the supported date formats. It only gives a few examples like "10 September 2000", "+1 week 2 days 4 hours 2 seconds", and "next Thursday".

Where can I find a complete description?

+6  A: 

I can't find anything official, but I saw a tutorial that says strtotime() uses GNU Date Input Formats. Those are described in detail in the GNU manual.

One discrepancy I notice is that "next" doesn't match the behaviour described in the GNU manual. Using strtotime(), "next Thursday" will give you the same result as "Thursday", unless today is a Thursday.

If today is a Thursday, then

  • strtotime("Thursday") == strtotime("today")
  • strtotime("next Thursday") == strtotime("today + 7 days")

If today is not a Thursday, then

  • strtotime("Thursday") == strtotime("next Thursday")

I'm using PHP 5.2.6.

Don Kirkby
I remember seeing something that said the same thing, so I'll give you a +1, but this seems a little like you're just trying to earn rep...
rmeador
I had to hunt a bit for this, so I posted the question and answer in case others were looking for the same thing. This is an intended use for the site, I think.
Don Kirkby
Yes, the FAQ explicitly states that answering your own questions is OK as long as it's in the format of Question<->Answer. If the question is stupid, it will get down voted. Otherwise, this will be useful to others searching for this info in the future.
Calvin
you should be able to post a question and answer without getting down votes. just wait a good while before choosing a correct answer, someone might do better then you. :)
OIS
A: 

In my experience strtotime() can accept anything that even remotely looks like a date. The best way to figure out its boundaries are is to create a test script and plug in values and see what does and doesn't work.

$input = "Apr 10th";
print(date('Y-m-d', strtotime($input)));

Just keep changing the $input variable and observe the results.

Michael Luton
+1  A: 

You can start to trace what it is doing by looking at the following C code:

http://cvs.php.net/viewvc.cgi/php-src/ext/date/php_date.c

Search for PHP_FUNCTION(strtotime)

Also this is the main regex parsing:

http://cvs.php.net/viewvc.cgi/php-src/ext/date/lib/parse_date.re

Good luck

JH
A: 

Basically anything that date can create strtotime will parse. With one exception, it does have issues with non-US style formatting. So keep it Month-Day-Year type formatting in place.

jtyost2