PHP Regular validation Date time format Instance Code _php Tutorial

Source: Internet
Author: User
Using PHP to verify the date format we can use the checkdate () function and regular expressions to verify, and I will give you a brief introduction to my classmates in the validation date format some process, and finally the simplest way.

1. Use the regular validation date time format
Mainly use PHP functions such as Ereg, Preg_match and so on.
There's a very simple,

The code is as follows Copy Code
$dateTime = "2010-6-4 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";
}

But this is only verified by the number, not the boundary value verification, not comprehensive, so someone wrote this regular

The code is as follows Copy Code

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 part
Regex + = "(S ((0?[ 0-9]) | ([1-2][0-3])):( [0-5]? [0-9]) ((s) | (:([0-5]? [0-9])))? $ "; Time section

This is simply the work of God.
So I had to give up using the regular to verify the legitimacy of the time format.

2. The time string is disassembled to verify the date-time format, respectively.
The main use of checkdate and other PHP functions.
This method is very good, the validation is accurate, but the code is difficult to write, you need to separate the date and time, the date using Checkdate ($k [1], $k [2], $k [0]) to verify that the time needs to be disassembled and checked.

In fact, can be combined with the above two methods to use, the operation is much more convenient.
1. The first use of the regular validation is the "2011-11-07 12:30:55" format.

Use

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

You can do it.
2. Then use the Strtotime () function to determine the validation, passing in the date string.
The Strtotime () function returns the UNIX timestamp corresponding to the specified datetime string by default.
A feature of the Strtotime () function is that it is convenient to return FALSE if the incoming date string is malformed, and to support various date formats.

http://www.bkjia.com/PHPjc/628719.html www.bkjia.com true http://www.bkjia.com/PHPjc/628719.html techarticle Using PHP to verify the date format we can use the checkdate () function and regular expressions to verify, and I will give you a brief introduction of my classmates in the validation date format some process, the final conclusion ...

  • 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.