Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What date formats does the PHP function strtotime() support?

Tags:

php

datetime

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?

like image 213
Don Kirkby Avatar asked Feb 28 '23 20:02

Don Kirkby


2 Answers

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.

Update:

I guess the user manual has been updated since I posted this, or else I was blind. It now contains a link to the Date and Time Formats chapter, that includes a section on relative formats.

like image 193
Don Kirkby Avatar answered Mar 03 '23 09:03

Don Kirkby


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

like image 41
JH. Avatar answered Mar 03 '23 09:03

JH.