CHAPTER 9 – DATE HANDLING – Parsing Date Formats

The opposite of formatting text is parsing a textual description of a date into a timestamp. The strtotime() function handles a many different formats. In addition to the formats listed at http://www.gnu.org/software/tar/manual/ html_chapter/tar_7.html, PHP also supports some extra ISO 8601 formats (http://www.w3.org/TR/NOTE-datetime). Table 9.7 contains a list of the most useful formats. Table 9.7 Date/Time Formats as Understood by strtotime() GMT Formatted Date String Date Remarks 1970-09-17 1970-09-16 23:00:00 ISO 8601 preferred date. 9/17/72 1972-09-16 23:00:00 Common U.S. way (d/m/yy). 24 September 1972 1972-09-23 23:00:00 Without any specified time, 0:00 is used. Because the time zone is set to 24 Sep 1972 1972-09-23 23:00:00 MET (GMT+1), the GMT formatted Sep 24, 1972 1972-09-23 23:00:00 date is in the previous day. 20:02:00 2004-03-01 19:02:00 Without any date specified, the cur- rent date is used. 20:02 2004-03-01 19:02:00 8:02pm 2004-03-01 19:02:00 20:02-0500 2004-03-02 01:02:00 -0500 is the time zone (EST). 20:02 EST 2004-03-02 01:02:00 Thursday 2004-03-03 23:00:00 A day name advances to the first 1 Thursday available day with this name. In this Thursday case the current day has this name, the current day is used. 2 Thursday 19:00 2004-03-11 18:00:00 2 is the second Thursday from now. next Thursday 7pm 2004-03-11 18:00:00 Next means the next available day with this name after the first avail- able day, and thus is the same as 2. last Thursday 19:34 2004-02-26 18:34:00 The Thursday before the current day. If the name of the day is the same as the current day, the time- stamp of the previous day is used. 1 year 2 days ago 2003-02-27 21:25:44 The current time is used to calcu- late the relative displacement with. -1 year -2 days 2003-02-27 21:25:44 The ­ sign is needed before every -1 year 2 days 2003-03-03 21:25:44 displacement unit; if it's not used, + is assumed. If "ago" is postfixed, 1 year -2 days 2005-02-27 21:25:44 the meaning of + and ­ is reversed. Other possible units are second, tomorrow 2004-03-02 21:25:44 minute, hour, week, Month, and yesterday 2004-02-29 21:25:44 fortnight (14 days). 20040301T00:00:00+1900 2004-02-29 05:00:00 Used for WDDX parsing. Table 9.7 Date/Time Formats as Understood by strtotime() GMT Formatted Date String Date Remarks 2004W021 2004-01-04 23:00:00 Midnight of the first day of ISO week 21 in 2004. 2004122 0915 2004-12-22 08:15:00 Only numbers in the form yyyymmdd hhmm. Using the strtotime() function is easy. It accepts two parameters: the string to parse to a timestamp and an optional timestamp. If the timestamp is included, the time is converted relative to the timestamp; if it's not included, the current time is used. The relative calculations are only written with yes- terday, tomorrow, and the 1 year 2 days (ago) format strings. strtotime() parsing is always done with the current time zone, unless a different time zone is specified in the string that is parsed: <?php echo date("H:i Tn", strtotime("09:22")); // shows 09:22 CET echo date("H:i Tnn", strtotime("09:22 GMT")); // shows 10:22 CET echo gmdate("H:i Tn", strtotime("09:22")); // shows 08:22 GMT echo gmdate("H:i Tn", strtotime("09:22 GMT")); // shows 09:22 GMT ?> For more information on time zones, times, and calendars, see the excel- lent web site at http://www.timeanddate.com/.

Post Comment
Login to post comments