preg_match() および checkdate() 関数を使用して日時形式を検証したい。私の形式は「dd/MM/yyyy hh:mm:ss」です。私のコードの何が問題なのですか?
function checkDatetime($dateTime){
$matches = array();
if(preg_match("/^(\d{2})-(\d{2})-(\d{4}) ([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $dateTime, $matches)){
print_r($matches); echo "<br>";
$dd = trim($matches[1]);
$mm = trim($matches[2]);
$yy = trim($matches[3]);
return checkdate($mm, $dd, $yy); // <- Problem here?
}else{
echo "wrong format<br>";
return false;
}
}
//wrong result
if(checkDatetime("12-21-2000 03:04:00")){
echo "checkDatetime true<br>";
}else{
echo "checkDatetime false<br>";
}
//correct result
if(checkdate("12", "21", "2000")){
echo "checkdate true<br>";
}else{
echo "checkdate false<br>";
}
出力:
Array ( [0] => 12-21-2000 03:04:00 [1] => 12 [2] => 21 [3] => 2000 [4] => 03 [5] => 04 [6] => 00 )
checkDatetime false
checkdate true