PHP regular match date and time stamp conversion case detailed

Source: Internet
Author: User
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>


The above is the whole content of this article, I hope that everyone's study has helped.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.