以下是一个比较两个日期时间以检查日期是否处于活动状态的PHP代码示例:
= $startDateTime && $currentDateTime <= $endDateTime) {
return true;
} else {
return false;
}
}
// 示例用法
$startDate = "2022-01-01";
$endDate = "2022-01-31";
$currentDate = date("Y-m-d"); // 当前日期
if (isDateActive($startDate, $endDate, $currentDate)) {
echo "活动正在进行中!";
} else {
echo "活动已结束或未开始。";
}
?>
在上述示例中,isDateActive
函数接受三个参数:$startDate
(活动开始日期),$endDate
(活动结束日期)和$currentDate
(要检查的日期)。函数内部将这些参数转换为DateTime
对象,并使用比较运算符将$currentDateTime
与$startDateTime
和$endDateTime
进行比较。如果$currentDateTime
在活动日期范围内,则返回true
,否则返回false
。
在示例的最后部分,我们使用示例日期来调用isDateActive
函数,并根据返回的结果输出相应的消息。