2013-05-08 41 views
0

我有一个计算离开了产品上市日至expire.When我运行下面的代码,我得到浮点数如3.4722222222222E-5减法日期收益浮动

$days_left = date('d', strtotime($this->expiry_date) - strtotime($date))/(60 * 60 * 24); 
$this->days_left = $days_left; 

当数函数我舍结果

(round)$this->days_left 

一个的愈来愈0

+2

尝试'round($ this-> days_left);'not'(round)$ this-> days_left'([manual](http://php.net/manual/ru/function.round.php)) – BlitZ 2013-05-08 11:58:29

+0

是否确定$ this-> expiry_date始终具有该值,请检查该值是否在您获得该种浮动结果 – kumar 2013-05-08 13:10:10

回答

0

strtotime($this->expiry_date) - strtotime($date)让你在几秒钟的时间差。

date('d', $time)会给你那一天$time的一部分,删除任何月份/年的部分。并且你已经在一天的数字中划分了每秒的秒数?显然,浮点会导致计算机盲目地进行分工。

相反,沟date函数来获取的天数,但在一天的秒数分配时间(秒):

$days_left = (strtotime($this->expiry_date) - strtotime($date))/(60 * 60 * 24); 

,你不“类型转换”到round这不存在。根据意见,做round($this->days_left)

+0

时感谢。它工作正常 – user1180807 2013-05-09 16:17:33

0

如果你想正确计算日期,你应该使用DateTime对象。日期之间的差异(东西的持续时间)应该呈现为DateInterval对象,这会给出确切的区别。