php - php 中的天数差异

标签 php mysql

我有一个函数可以计算两个日期之间的差异。

function getDateDifference($to, $from, $in) {
$diff = abs($to - $from);

$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));
if ($in == "days") {
    return $days;
} else if ($in == "months") {
    return $months;
} else if ($in == "years") {
    return $years;
}

对于参数我首先像这样把两个日期转换成秒,

checkin = '2012-07-26';
checkout = '2012-07-27';
check_in_date = strtotime(checkin);
check_out_date = strtotime(checkout);

当谈到不到一个月的差异时,我得到了正确的差异。但如果差异超过 1 个月,我总是得到 1 的差异。谁能告诉我问题出在哪里。

最佳答案

目前,一个月总是 30 * 60 * 60 * 24 秒,也就是 30 天。

您的问题是我们在 7 月,有 31 天,而不是 30 天。您必须注意每个月的天数。

关于php - php 中的天数差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11666902/

相关文章:

php - 在 Notepad++ 中使用双击捕获 PHP 变量

php - 当减去年份时,它返回不同的结果

php - 显示所有带有 php inner join 的帖子

php - foreach输入php和mysql

PHP MYSQL 上传文件并存储为 blob

PHP/html 电子邮件表单不再提交到 mysql 数据库

php - 如何用php过滤和重组json数据?

mysql - MySQL中group by语句后选择对应的非聚合列

mysql - 替换 MYSQL 数据库列中所有出现的字符串

c++ - mysql-connector-c++ 8.0 连接被拒绝