我的日期格式为 MM-YYYY(例如:04-2000)。我将日期分解为月份和年份,现在尝试检查该月份的某些条件:
以及年份:
我的语法正确吗?
list($name_month, $name_year) = explode('-', $name_date, 2);
if(($name_month < 1 || $name_month > 12 || $name_month ) || ($name_year)) {
echo "<br><br>Wrong date";
$uploadOk = 0;}
Run Code Online (Sandbox Code Playgroud)
您可以使用 DateTime 对象并createFromFormat()验证您的日期:
$date = '04-2000';
// Create a DateTime object pointing to the 1st of your given month and year
$d = DateTime::createFromFormat('d-m-Y', '01-' . $date);
if($d && $d->format('m-Y') == $date){
echo 'Valid date';
}
Run Code Online (Sandbox Code Playgroud)