PHP DateDiff 问题
使用下面的 PHP 代码,我希望得到“2”作为我的输出。但我得到“1”。
有谁知道这是为什么?
$returndate = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3-$2-$1', '2011-03-28');
$departdate = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3-$2-$1', '2011-03-26');
$diff = abs(strtotime($returndate) - strtotime($departdate));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
echo $days; // expecting 2, but get 1
非常感谢您的帮助。
Using the PHP code below, I would expect to get '2' as my output. But I get '1'.
Does anyone know why this is?
$returndate = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3-$2-$1', '2011-03-28');
$departdate = preg_replace('#(\d+)/(\d+)/(\d+)#', '$3-$2-$1', '2011-03-26');
$diff = abs(strtotime($returndate) - strtotime($departdate));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
echo $days; // expecting 2, but get 1
Many thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如此多的计算...我认为这是您遇到的舍入问题,舍入所有时间测量...这是您正在做的事情的更简单的外观:
So much calculations... I assume its a rounding issue you have, rounding all the time measurements... here is a simpler look on what you are doing:
输出:
2
Output:
2