我在我的网站上显示特定事件开始前的天数和小时数,我创建了以下代码:
我的问题:
当没有天数时,天数和小时数会减去(小于零)而且没有任何小时数应该显示
//cur date
$curDate = date("Y-m-d");
//future date + 7days
$date = date('Ymd');
$date = strtotime($date);
$date = strtotime("+7 day", $date);
$date = date('Y-m-d', $date);
//return days and hours remaining until KO
$calcDate = $gameDate.$time;
$calcDate = strtotime(str_replace('/', '-', $gameDate));
$remaining = $calcDate - time();
$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600);
//echo in index.php display in if statments
if($days_remaining<0&&$hours_remaining<0){
echo'';
}
if($days_remaining == 0){
echo 'Starts In: <span class="timeSpan">'.$hours_remaining.'</span> Hours';
}
else if($days_remaining != 0){
echo' Starts In: <span class="timeSpan">'.$days_remaining.'</span> Days And <span class="timeSpan">'.$hours_remaining.'</span>Hours';
}
答案 0 :(得分:1)
替换:
if($days_remaining == 0){
使用:
if($days_remaining <= 0){
通过这种方式,您将检查剩余天数是0还是小于0,并且我相信您必须为我在代码中看不到的小时数添加类似的检查,但应该是相同的条件。
答案 1 :(得分:0)
当$days_remaining
AND $hours_remaining
小于0时,您仍会转到上一个if/else
语句。由于如果两者都小于零,你没有显示任何内容,只有当其中一个大于零时,我才会进入最后if/else
:
if($days_remaining>0 || $hours_remaining>0){
// You have something to display
if($days_remaining <= 0){
// Something with only hours
echo 'Starts In: <span class="timeSpan">'.$hours_remaining.'</span> Hours';
}
else {
// Something with days and hours
echo' Starts In: <span class="timeSpan">'.$days_remaining.'</span> Days And <span class="timeSpan">'.$hours_remaining.'</span>Hours';
}
}