0

我正在尝试编写一个函数来检查“完成的课程”是否是四天前。例如,我如何检查所述课程是否在该时间范围内。如果昨天完成,2天前,3天前,4天前,那是真的,因为它在“4天前”的时间范围内。

我该如何检查?

到目前为止,我已经完成了:

$time = time();

$fourDays = 345600;
$threeDays = 259200;
$lastLesson = $ml->getLesson($cid, $time, true);

$lastLessonDate = $lastLesson['deadline'];
$displayLastLesson = false;
if ($lastLessonDate  + $fourDays < $time)
{
    $displayLastLesson = true;
    //We print lesson that was finished less than 4 days ago
}
else
{
    //We print lesson that is in the next 3 days

}

现在,if 语句一直为真,这不是我想要的,因为我有一个在 5 月 3 日完成的课程。对于 7 日完成的课程,我猜应该是这样吧?

4

2 回答 2

2
$time = time();
$fourDays = strtotime('-4 days');
$lastLesson = $ml->getLesson($cid, $time, true);

$lastLessonDate = $finishedLesson['deadline'];
$displayLastLesson = false;
if ($lastLessonDate >= $fourDays && $lastLessonDate <= $time)
{
    $displayLastLesson = true;
    //We print lesson that was finished less than 4 days ago
}
else
{
    //We print lesson that is in the next 3 days

}
于 2012-05-10T11:33:50.517 回答
0

所有计算都应相对于今天上午 12 点计算,而不是time()为您提供现在的当前时间(例如下午 6 点) 这是一个问题,因为当您这样做时,1 天前(现在 - 24 小时)表示昨天下午 6 点和今天之间的时间下午 6 点。相反,昨天应该是指昨天凌晨 12 点到今天凌晨 12 点之间的时间。

下面是一个简化的计算来说明这个想法:

$lastLessonDate = strtotime($lastLessonDate);
$today = strtotime(date('Y-m-d')); // 12:00am today , you can use strtotime('today') too
$day = 24* 60 * 60;
if($lastLessonDate > $today) // last lesson is more than 12:00am today, meaning today
 echo 'today';
else if($lastLessonDate > ($today - (1 * $day))
 echo 'yesterday';
else if($lastLessonDate > ($today - (2 * $day))
 echo '2 days ago';
else if($lastLessonDate > ($today - (3 * $day))
 echo '3 days ago';
else if($lastLessonDate > ($today - (4 * $day))
 echo '4 days ago';
else
 echo 'more than 4 days ago';
于 2012-05-10T11:52:59.897 回答