Home >Backend Development >PHP Tutorial >PHP uses strtotime and date functions to determine whether the date is valid code sharing_PHP tutorial
At first glance, it should be a simple function to determine whether the date is valid, but when you think about it, it is still a bit troublesome, because you need to check both the format and the validity. For example, 2013-02-29, although the format is correct, the date is invalid; and 2012-02-29, the format is correct and valid.
One method can use regularization, but regularization is actually quite troublesome to understand, and using regularization is not very good at testing effectiveness. Here is a method, mainly using strtotime and date functions for verification. Directly access the function:
//Verify the validity of the date, as long as it meets one of the formats
foreach ($formats as $format) {
if (date($format, $unixTime) == $date) {
return true;
The code comments explain it in more detail, so I won’t go into details. One thing to note: If the required date format is relatively special, even if it is in the correct format, the strtotime function cannot parse it, so this function cannot be used, but this situation should be very rare.
Some examples:
var_dump(checkDateIsValid("2013-09-10")); //output true
http: //www.bkjia.com/PHPjc/621665.html