3.9.1. ProblemYou need to get a date or time in a string into a format you can use in calculations. For example, you want to convert date expressions such as "last Thursday" into an epoch timestamp. 3.9.2. SolutionThe simplest way to parse a date or time string of arbitrary format is with strtotime( ), which turns a variety of human-readable date and time strings into epoch timestamps, as shown in Example 3-20. Parsing strings with strtotime( )
3.9.3. DiscussionThe grammar strtotime( ) uses is both complicated and comprehensive. It uses the GNU Date Input Formats specification, which is available at the following address: http://www.gnu.org/software/coreutils/manual/html_chapter/coreutils_27.html. The function strtotime( ) understands words about the current time: <?php $a = strtotime('now'); print strftime('%c',$a); $a = strtotime('today'); print strftime('%c',$a); ?> Mon Aug 12 20:35:10 2002 Mon Aug 12 20:35:10 2002 It understands different ways to identify a time and date: <?php $a = strtotime('5/12/1994'); print strftime('%c',$a); $a = strtotime('12 may 1994'); print strftime('%c',$a); ?> Thu May 12 00:00:00 1994 Thu May 12 00:00:00 1994 It understands relative times and dates: <?php $a = strtotime('last thursday'); // On August 12, 2002 print strftime('%c',$a); $a = strtotime('2001-07-12 2pm + 1 month'); print strftime('%c',$a); ?> Thu Aug 8 00:00:00 2002 Mon Aug 12 14:00:00 2002 It understands time zones. When the following is run from a computer in EDT, it prints out the same time: <?php $a = strtotime('2002-07-12 2pm edt + 1 month'); print strftime('%c',$a); ?> Mon Aug 12 14:00:00 2002 However, when the following is run from a computer in EDT, it prints out the time in EDT when it is 2 P.M. in MDT (two hours before EDT): <?php $a = strtotime('2002-07-12 2pm mdt + 1 month'); print strftime('%c',$a); ?> Mon Aug 12 16:00:00 2002 If the date and time you want to parse out of a string are in a format you know in advance, instead of calling strtotime( ), you can build a regular expression that grabs the different date and time parts you need. Example 3-21 shows how to parse "YYYY-MM-DD HH:MM:SS" dates, such as a MySQL DATETIME field. Parsing a date with a regular expression
This puts the year, month, day, hour, minute, and second into $date_parts[1] tHRough $date_parts[6]. (preg_match( ) puts the entire matched expression into $date_parts[0].) You can use regular expressions to pull the date and time out of a larger string that might also contain other information (from user input, or a file you're reading), but if you're sure about the position of the date in the string you're parsing, you can use substr( ) to make it even faster, as shown in Example 3-22. Parsing a date with substr( )
You can also use preg_split( ), as in Example 3-23. Parsing a date with preg_split( )
Example 3-23 prints: array(6) { [0]=> string(4) "1974" [1]=> string(2) "12" [2]=> string(2) "03" [3]=> string(2) "05" [4]=> string(2) "12" [5]=> string(2) "56" } Be careful: PHP converts between numbers and strings without any prompting, but numbers beginning with a 0 are considered to be in octal (base 8). So 03 and 05 are 3 and 5, but 08 and 09 are not 8 and 9. In PHP 5.1 and later, preg_match( ) is faster than strtotime( ) in parsing a date format such as "YYYY-MM-DD HH:MM:SS." In earlier versions of PHP, strtotime( ) is slightly faster. If you need the individual parts of the date string, preg_match( ) is more convenient, but strtotime( ) is obviously much more flexible. 3.9.4. See AlsoDocumentation on strtotime( ) at http://www.php.net/strtotime. The rules describing what strtotime( ) can parse are at http://www.gnu.org/software/coreutils/manual/html_chapter/coreutils_27.html. |