使用 php 的 2 个日期之间的天数差异
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
但我不明白为什么,因为在 print_r 中我有 Date Now :2015-12-04
和 Last Win:2015-12-03
你能帮我看看我哪里出错了吗?提前致谢,对不起我的英语
这个结果是正确的,因为你先得到两个日期之间的秒数,然后用它除以 24 小时内的秒数 (86400),结果是 1(天)。
一天有24小时,每小时有60分钟,每分钟有60秒。因此,一天有24*60*60 = 86400sec
现在,strtotime()
函数将英文文本日期时间解析为 Unix 时间戳(自格林威治标准时间 00:00:00 1970 年 1 月 1 日以来的秒数)。意思是 returns 秒。
所以,i_now
= 1449187200 和 i_date_last_bonus
= 1449100800
相差 86400 秒。
现在 $i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400);
这是将秒数转换为天数。
差异是86400 / 86400 = 1
表示 1 天。
我的脚本有问题,我不明白问题出在哪里。所以我有这段代码:
$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
但我不明白为什么,因为在 print_r 中我有 Date Now :2015-12-04
和 Last Win:2015-12-03
你能帮我看看我哪里出错了吗?提前致谢,对不起我的英语
这个结果是正确的,因为你先得到两个日期之间的秒数,然后用它除以 24 小时内的秒数 (86400),结果是 1(天)。
一天有24小时,每小时有60分钟,每分钟有60秒。因此,一天有24*60*60 = 86400sec
现在,strtotime()
函数将英文文本日期时间解析为 Unix 时间戳(自格林威治标准时间 00:00:00 1970 年 1 月 1 日以来的秒数)。意思是 returns 秒。
所以,i_now
= 1449187200 和 i_date_last_bonus
= 1449100800
相差 86400 秒。
现在 $i_datediff = round(abs($i_now - $i_date_last_bonus) / 86400);
这是将秒数转换为天数。
差异是86400 / 86400 = 1
表示 1 天。