您可以
DateTime为此目的使用类:
function validateDate($date, $format = 'Y-m-d'){ $d = DateTime::createFromFormat($format, $date); // The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue. return $d && $d->format($format) === $date;}
测试用例:
var_dump(validateDate('2013-13-01')); // falsevar_dump(validateDate('20132-13-01')); // falsevar_dump(validateDate('2013-11-32')); // falsevar_dump(validateDate('2012-2-25')); // falsevar_dump(validateDate('2013-12-01')); // truevar_dump(validateDate('1970-12-01')); // truevar_dump(validateDate('2012-02-29')); // truevar_dump(validateDate('2012', 'Y')); // truevar_dump(validateDate('12012', 'Y')); // false
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)