Php uses the strtotime and date functions to determine whether the date is valid for code sharing

Source: Internet
Author: User
Php uses the strtotime and date functions to test and judge whether the date is valid for code sharing. For more information, see.

Php uses the strtotime and date functions to test and judge whether the date is valid for code sharing. For more information, see.

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, 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

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.