Home > Article > Backend Development > How to solve the problem of -0 in php countdown
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('start-now'=>$b), array('float_day'=>$c), array('int_day'=>$d), array('off'=>$off)); if (-0 < 0) { echo '-0 < 0'; } else { echo '-0 > 0'; }
Output:
array 'start-now' => int -33321 array 'float_day' => float -0.385659722222 array 'int_day' => float -0 array 'off' => float -0 -0 > 0
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); }
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 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!