In this paper, the use of PHP to implement the regular matching date and time, and time stamp conversion example, the need for friends, refer to the bar
First, a simpler and more practical code
Date Yyyy-mm-dd
$str = "; $isMatched = Preg_match ('/^\d{4} (\-|\/|.) \d{1,2}\1\d{1,2}$/', $str, $matches); Var_dump ($isMatched, $matches);
PHP requires a certain amount of time format to be converted to a timestamp (representing the total number of seconds from GMT January 01, 1970 00:00 00 seconds), which will be used in PHP regular judgment, the following is the code:
The <?php//match time format is 2016-02-16 or 2016-02-16 23:59:59 before 0 can be written without writing the page = "2016-12-14 23:59:59"; $patten = "/^\d{4}[\-" (0?[ 1-9]|1[012]) [\-] (0?[ 1-9]| [12] [0-9]|3[01]) (\s+ (0?[ 0-9]|1[0-9]|2[0-3]) \:(0? [0-9]| [1-5] [0-9]) \:(0? [0-9]| [1-5] [0-9])? $/", if (Preg_match ($patten, $time)) {echo $timestro = Strtotime ($time);} else {echo" error ";}? >
The analysis of the regular:
/^\d{4}[\-] (0?[ 1-9]|1[012]) [\-] (0?[ 1-9]| [12] [0-9]|3[01]) (\s+ (0?[ 0-9]| [12] [0-3]) \:(0? [0-9]| [1-5] [1-9]) \:(0? [0-9]| [1-5] [1-9])? $/
/is the terminator of the expression at the very back of the start character of the expression
^ Start of the string, that is, start with * * *
$ represents the end of the string, which means that the end of the
| indicates or
() represents a unit
D{4} matches a 4-digit number,
(\-) matches a "-" number,
(0?) [1-9]|1[012]) matches the month,
(0?) [1-9]| [12] [0-9]|3[01]) matches the day,
(\s+ (0?[ 0-9]|1[0-9]|2[0-3]) \:(0? [0-9]| [1-5] [0-9]) \:(0? [0-9]| [1-5] [0-9])? The whole match is a minute and a half.
The final step is to make a timestamp conversion to the part that matches the success.
The current conversion result is: 1329407999
Attached to my own example, the code is as follows:
<meta charset= ' utf-8 ' > <pre> <?php $str = "Hey, 2010/07/08 12:31:56 haha 1999/12/31 13:21:45"; $str 2 = "Hey 2010-07-08 12:31:56 haha 1999-12-31 13:21:45"; Match date only Preg_match_all ("/d{4}/d{2}/d{2}/", $str, $arr); Preg_match_all ("/d{4}-d{2}-d{2}/", $str 2, $arr 2); Match date and time Preg_match_all ("/d{4}/d{2}/d{2}sd{2}:d {2}:d {2}/", $str, $arr 3); Preg_match_all ("/d{4}-d{2}-d{2}sd{2}:d {2}:d {2}/", $str 2, $arr 4); Echo ' <font color= ' red ' > Match date </font><br> '; Print_r ($arr); Print_r ($arr 2); Echo ' <font color= ' red ' > Match date and Time </font><br> '; Print_r ($arr 3); Print_r ($arr 4);?> </pre>