Home > Backend Development > PHP Tutorial > How to solve the problem of -0 in php countdown

How to solve the problem of -0 in php countdown

墨辰丷
Release: 2023-03-29 09:24:01
Original
1631 people have browsed it

This article mainly introduces how to solve the problem of -0 when the PHP countdown program appears. The example analyzes the reasons why the PHP countdown program appears -0 and the corresponding solutions. It has certain reference value. Friends who need it can refer to it

The details are as follows:

Question:There was feedback today that the countdown showed -0 days. I looked at the program and, damn, I didn’t test it at the time

The reason is that PHP's logical judgment is -0 > 0

Analysis: Post the wrong code

$starttime   = 1362585600; //3.7凌晨
$nowtime   = 1362618921;//3.7早上
$off = ceil(($starttime - $nowtime)/86400); //倒计时
if ($off < 0) {
  $off = 0;
}
$b = $starttime - $nowtime;
$c = $b/86400;
$d = ceil($c);
var_dump(array(&#39;start-now&#39;=>$b), array(&#39;float_day&#39;=>$c), array(&#39;int_day&#39;=>$d), array(&#39;off&#39;=>$off));
if (-0 < 0) {
  echo &#39;-0 < 0&#39;;
} else {
  echo &#39;-0 > 0&#39;;
}
Copy after login

Output:

array
 &#39;start-now&#39; => int -33321
array
 &#39;float_day&#39; => float -0.385659722222
array
 &#39;int_day&#39; => float -0
array
 &#39;off&#39; => float -0
-0 > 0
Copy after login

Process:

When the start time and the current time are the same day, the above Since -0 > 0, the calculation process will be off = -0.

Improvement:

$starttime   = 1362585600; //3.7凌晨
$nowtime   = 1362618921;//3.7早上
if (($starttime - $nowtime) < 0) {
  $off = 0;
} else {
  $off = ceil(($starttime - $nowtime)/86400);
}
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations: Detailed explanation of the usage of final keyword in

php

php Usage of static and const keywords

phpDetailed analysis of usage of this keyword

The above is the detailed content of How to solve the problem of -0 in php countdown. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template