PHP date() date_create() strtotime() options

Author:
phil
Created:
Saturday, April 07th, 2012
Last Updated:
Wednesday, October 02nd, 2019

Disclaimer: Accessing the information on this page means you agree to the Sites Terms of Service


OK, so I linked to the following information on a Drupal help page, thinking the page would be available for at least the next 10 years... I was wrong. It's gone. Doing some digging though I was fortunately able to find the information again and decided this time to copy it and archive it for my own records in case the new location decides to vanish as well...

I think some of this information might have been copied from somewhere else, so I'll reference the site I found it on and the places they claim to have copied the info from as well... At this point, I have no idea who gets the credit...

After some digging, I finally ran across the actual PHP manual information regarding a lot of this topic:

strtotime()

Moving on, if you are looking for a quick overview of what you can use for date() options, date_create() options or strtotime() options such as +1 month, +3 days etc, take a look below.


strtotime()

PHP's extremely convenient strtotime() function is adapted from the get_date GNU library. It can convert myriad textual human representations of dates/times into Unix timestamps.

This can be very useful when converting between any of the common date formats above, especially when encountering non- standards-compliant data.

Some examples:

    strtotime('2003-07-30 -1 month');
    strtotime('1972-09-24');
    strtotime('72-9-24');
    strtotime('72-09-24');
    strtotime('24 September 1972');
    strtotime('24 Sept 72');
    strtotime('24 Sep 72');
    strtotime('Sep 24, 1972');
    strtotime('24-sep-72');
    strtotime('24sep72');
    strtotime('24-sep-72 20:02');
    strtotime('24-sep-72 8:02pm');
    strtotime('1 year');
    strtotime('1 year ago');
    strtotime('3 years');
    strtotime('2 days');
    strtotime('-1 month');
    strtotime('now');
    strtotime('+1 week');
    strtotime("+1 week 3 days 2 hours 8 seconds");
    strtotime('next Thursday');
    strtotime('last Monday');

Some units of time that strtotime() understands:

  • am: the time is before noon
  • pm: the time is noon or later
  • year: one year; for example, "next year"
  • month: one month; for example, "last month"
  • fortnight: two weeks; for example, "a fortnight ago"
  • week: one week
  • day: a day
  • hour: an hour
  • minute: a minute
  • min: same as minute
  • second: a second
  • sec: same as second

Some relative and ordinal words that strtotime() understands:

  • ago: past time relative to now; such as "24 hours ago"
  • tomorrow: 24 hours later than the current date and time
  • yesterday: 24 hours earlier than the current date and time
  • today: the current date and time
  • now: the current date and time
  • last: modifier meaning "the preceding"; for example, "last tuesday"
  • this: the given time during the current day or the next occurrence of the given time; for example, "this 7am" gives the timestamp for 07:00 on the current day, while "this week" gives the timestamp for one week from the current time
  • next: modifier meaning the current time value of the subject plus one; for example, "next hour"
  • first: ordinal modifier, esp. for months; for example, "May first" (actually, it's just the same as next)
  • third: see first (note that there is no "second" for ordinality, since that would conflict with the second time value)
  • fourth, fifth, sixth...

Usage tips:

  • For 2 digit years, strtotime() assumes 19xx for 69-99 and 20xx for 00-68
  • US month/day/year format is acceptable, though it may be ambiguous for many dates if the months are numerical rather than names or abbreviations.
  • Always number time interval units such as days, weeks, etc. using numerals rather than spelled out; strtotime() can understand "2 weeks ago" but cannot understand "two weeks ago".
  • There are frustrating subtle differences in the flexibility of strtotime() in different version of PHP. If you think you might be testing the limits of what strtotime() can parse, be sure to test, test, test.

Original Article

Original Referenced:

(Some examples in this section borrowed from the GNU tar manual, copyright © 2004 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.; word specifier lists adapted from PHP Functions Essential Reference, copyright © 2002 New Riders Publishing.)

Post Comment