The Mysolutions

20Apr/110

PHP 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.

Filed under: strtotime Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.