本文實(shí)例講述了php倒計(jì)時(shí)出現(xiàn)-0情況的解決方法。分享給大家供大家參考,具體如下:
問(wèn)題:今天有反饋,說(shuō)倒計(jì)時(shí)出現(xiàn)了-0天的情況,我看了看程序,臥槽,當(dāng)時(shí)怎么沒測(cè)試到
原因是PHP的邏輯判斷中 -0 > 0
分析:貼出錯(cuò)的代碼
$starttime = 1362585600; //3.7凌晨$nowtime = 1362618921;//3.7早上$off = ceil(($starttime - $nowtime)/86400); //倒計(jì)時(shí)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';}輸出:
array 'start-now' => int -33321array 'float_day' => float -0.385659722222array 'int_day' => float -0array 'off' => float -0-0 > 0
過(guò)程:
當(dāng)開始時(shí)間和當(dāng)前時(shí)間是同一天時(shí),上邊的計(jì)算過(guò)程由于 -0 > 0 所以會(huì)出現(xiàn) off = -0 的情況
改進(jìn):
$starttime = 1362585600; //3.7凌晨$nowtime = 1362618921;//3.7早上if (($starttime - $nowtime) < 0) { $off = 0;} else { $off = ceil(($starttime - $nowtime)/86400);}希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選