[英]php time and hours less than zero (0)
Im displaying on my website the amount of days and hours until a certain event starts, I created the following code: 我在网站上显示直到某个事件开始的天数和小时数,我创建了以下代码:
MY PROBLEM: 我的问题:
Days and hours goes in the minus (less than zero) when there is no days and no hours remaining nothing should be displayed 当没有天且没有剩余小时数时,天数和小时数将减去(小于零)。
//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';
}
Replace: 更换:
if($days_remaining == 0){
With: 附:
if($days_remaining <= 0){
This way you will check if the remaining days are 0 or less than 0, and I believe that you will have to add a similar check for the hours that I did not see in your code, but should be the same condition. 这样,您将检查剩余的天数是否为0或小于0,并且我相信您必须为我在代码中未看到的小时数添加类似的检查,但条件应相同。
When $days_remaining
AND $hours_remaining
are less than 0, you still go to the last if/else
statement. 当
$days_remaining
和$hours_remaining
小于0时,您仍然转到最后的if/else
语句。 As you are not displaying anything if both are less than zero, I'll go in that last if/else
only if one of them is more than zero: 由于如果两者都不小于零,则不会显示任何
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';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.