例如,我的日期/时间范围没有年份,需要转换为时间戳,如下所示,
<?php
$dateStart = '11-28 00:00:00';
$dateEnd = '11-28 23:59:59';
?>
现在我想获取用户的当前日期和时间,并检查它是否属于给定的日期范围。我该怎么做?
答案 0 :(得分:3)
您可以使用此逻辑:
$start_date = '2012-07-10';
$end_date = '2012-10-06';
$date_from_user = '2009-08-28';
checkInRange($start_date, $end_date, $date_from_user);
function checkInRange($start_date, $end_date, $date_from_user)
{
// Convert to timestamp
$start_timestamp = strtotime($start_date);
$end_timestamp = strtotime($end_date);
$user_timestamp = strtotime($date_from_user);
// Check that user date is between start & end
return (($user_timestamp >= $start_timestamp) && ($user_timestamp <= $end_timestamp));
}
希望这有帮助!