php判断一个字符串是否为日期格式
<?php function validateDate($date, $format = 'Y-m-d H:i:s') { $d = DateTime::createFromFormat($format, $date); return $d && $d->format($format) == $date; } var_dump(validateDate("2021-12-30", "Y-m-d")); /** 输出: bool(true) */ ?>
<?php function validateDate($date, $format = 'Y-m-d H:i:s') { $d = DateTime::createFromFormat($format, $date); return $d && $d->format($format) == $date; } var_dump(validateDate("2021-12-30", "Y-m-d")); /** 输出: bool(true) */ ?>