Php uses the strtotime and date functions to determine whether the date is valid. code sharing _ PHP Tutorial

Source: Internet
Author: User
Php uses the strtotime and date functions to determine whether the date is valid for code sharing. In my opinion, judging whether a date is valid should be a simple function, but it is still a little troublesome to think about it, because we need to check both the format and the validity. For example, the question of-2 is that it should be a simple function to judge whether the date is valid, but it is still a little troublesome to think about it, because it is necessary to check the format and validity. For example, although the format is correct, but the date is invalid; while is also valid.

One method can use regular expressions, but regular expressions are quite difficult to understand, and they are not very good at verifying validity. Here we provide a method, mainly to use the strtotime and date functions for testing. Direct function:

The code is as follows:


/**
* Check whether the date format is correct
*
* @ Param string $ date
* @ Param string $ formats the format array to be verified
* @ Return boolean
*/
Function checkDateIsValid ($ date, $ formats = array ("Y-m-d", "Y/m/d ")){
$ UnixTime = strtotime ($ date );
If (! $ UnixTime) {// The strtotime conversion is incorrect. the date format is obviously incorrect.
Return false;
}

// Verify the validity of the date, as long as one of the formats is satisfied, OK
Foreach ($ formats as $ format ){
If (date ($ format, $ unixTime) = $ date ){
Return true;
}
}

Return false;
}

The descriptions in the code comments are more detailed and will not be described. Note: If the required date format is special, the strtotime function cannot be parsed even if it is in the correct format, but this function cannot be used, but this situation should be rare.

Some examples:

The code is as follows:


Var_dump (checkDateIsValid ("2013-09-10"); // outputs true
Var_dump (checkDateIsValid ("2013-09-ha"); // outputs false
Var_dump (checkDateIsValid ("2012-02-29"); // outputs true
Var_dump (checkDateIsValid ("2013-02-29"); // outputs false

Bytes. For example, 2013-02-2...

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.