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

How to solve the problem of -0 in php countdown

墨辰丷
墨辰丷Original
2018-06-01 16:14:231567browse

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;;
}

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

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

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!

Statement:
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