Php regular expression verification date and time format instance code _ PHP Tutorial

Source: Internet
Author: User
Tags php regular expression
Php regular expression verification date and time format instance code. Using php to verify the date format, we can use the checkdate () function and regular expression to verify the format. next I will introduce some of my processes in verifying the date format, finally, we can use php to verify the date format. we can use the checkdate () function and regular expression to verify the format. next I will introduce some of my processes in verifying the date format, finally, the simplest method is obtained.

1. use regular expression to verify the date and time format
It mainly uses php functions such as ereg and preg_match.
There is a very simple,

The code is as follows:
$ DateTime = "00:00:00 ″;
If (preg_match ("/^ d {4}-d {2}-d {2} d {2}: d {2}: d {2} $/s ", $ dateTime ))
{
Echo "Yes ";
} Else {
Echo "No ";
}

However, this regular expression only verifies the number, does not verify the boundary value, and is not comprehensive. so someone wrote this regular expression.

The code is as follows:

Regex = "^ (d {2} ([02468] [048]) | ([13579] [26]) [-/s]? (0? [13578]
) | (1 [02]) [-/s]? (0? [1-9]) | ([1-2] [0-9]) | (3 [01]) | (0? [4
69]) | (11) [-/s]? (0? [1-9]) | ([1-2] [0-9]) | (30) | (0? 2 [-/
S]? (0? [1-9]) | ([1-2] [0-9]) | (d {2} ([02468] [1235679]) | ([1
3579] [01345789]) [-/s]? (0? [13578]) | (1 [02]) [-/s]? ((
0? [1-9]) | ([1-2] [0-9]) | (3 [01]) | (0? [469]) | (11) [-/s]? ((
0? [1-9]) | ([1-2] [0-9]) | (30) | (0? 2 [-/s]? (0? [1-9]) | (1 [0-9]
) | (2 [0-8]) "; // date section
Regex + = "(s (0? [0-9]) | ([1-2] [0-3]) :( [0-5]? [0-9]) (s) | ([0-5]? [0-9])? $ "; // Time part

This is just a work of God.
So I had to discard the regular expression to verify the validity of the time format.

2. disassemble the time string to verify the date and time formats respectively.
It mainly uses php functions such as checkdate.
This method is good and accurate, but the code is difficult to write. you need to split the date and time separately. the date uses checkdate ($ k [1], $ k [2], $ k [0]) verification, and time needs to be split and checked one by one.

In fact, the above two methods can be integrated for use, and the operation is much more convenient.
1. First, use regular expressions to verify whether the format is "12:30:55.

Use

The code is as follows:
Preg_match ("/^ d {4}-d {2}-d {2} d {2}: d {2}: d {2} $/s ", $ dateTime)

You can.
2. then use the strtotime () function to judge and verify, and pass in the date string.
The strtotime () function returns the UNIX timestamp corresponding to the specified date and time string by default.
The strtotime () function has a feature, that is, if the input date string format is incorrect, false is returned, and various date formats are supported, which is very convenient.

The validate () function and the regular expression are used for verification. next I will introduce some processes of date format verification to you, and finally come to the conclusion...

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.