使用php的两个日期之间的差异(以天为单位)


Difference between 2 dates in days using php

我的脚本有问题,我不明白问题出在哪里。所以我有这个代码:

 $i_now = strtotime(date('Y-m-d'));
 $i_date_last_bonus = strtotime($o_member->date_last_bonus);                
 $i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400);
 print_r("Date Now :".date('Y-m-d'));
 print_r("Last Win :".$o_member->date_last_bonus);

我得到了$i_datediff = 1,我不明白为什么,因为在打印中我有Date Now :2015-12-04Last Win:2015-12-03你能帮我在哪里出错吗?提前,很抱歉我的英语

一天有24小时,每小时有60分钟,每分钟有60秒。因此,一天内有24*60*60=86400秒。

现在,strtotime()函数将英文文本日期时间解析为Unix时间戳(自1970年1月1日00:00:00 GMT以来的秒数)。意味着它返回秒。

因此,i_now=1449187200和i_date_last_bonus=1449100800差86400秒。

现在$i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400);,它正在以天为单位转换秒。

差为86400 / 86400 = 1表示1天。

这个结果是正确的,因为你首先得到两个日期之间的秒数,然后除以24小时内的秒数(86400),结果是1(天)。