PHP regular expression matching date and time program code _ PHP Tutorial

Source: Internet
Author: User
PHP regular expression matching date and time program code. Today, I want to replace a database. I want to extract the date and time in the specified string from the database and then convert the timestamp, my field is a character with a date. the matching time format is or today I want to replace a database. I want to extract the date and time in the specified string of the database and then convert the timestamp, my field is a character with a date. when the matching time format is or 23:59:59 is 0, I can leave it empty. then I found some test code.

The following code is provided:

The code is as follows:

// If the matching time is in the format of or 23:59:59, it can be left blank
$ Time = "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 ";
}
?>

Analysis of this regular expression:

The code is as follows:

/^ 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 end of the expression start character./Is the End character of the expression.
^ Start of the string, that is, start ***
$ Indicates the end of the string, that is, the end ***
| Represents or
() Indicates a unit.
D {4} matches a 4-digit number,
(-) Matches,
(0? [1-9] | 1 [012]) match month,
(0? [1-9] | [12] [0-9] | 3 [01]) matching 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 entire matching time, minute, and second part ,? Indicates either or not.

The last step is to convert the timestamp for successful matching.

Current conversion result: 1329407999

Attach a self-written instance

The code is as follows:



$ Str = "2010/07/08 12:31:56 haha 13:21:45 ";
$ Str2 = "2010-07-08 12:31:56 haha 13:21:45 ";
// Match only the date
Preg_match_all ("/d {4}/d {2}/d {2}/", $ str, $ arr );
Preg_match_all ("/d {4}-d {2}-d {2}/", $ str2, $ arr2 );
// Match the date and time
Preg_match_all ("/d {4}/d {2}/d {2} sd {2}: d {2}: d {2}/", $ str, $ arr3 );
Preg_match_all ("/d {4}-d {2}-d {2} sd {2}: d {2}: d {2}/", $ str2, $ arr4 );
Echo 'matching date
';
Print_r ($ arr );
Print_r ($ arr2 );
Echo 'match the date and time
';
Print_r ($ arr3 );
Print_r ($ arr4 );
?>

...

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.