DateInterval 浮点型总天数

DateInterval total number of days as float

现在我不关心时区,只想计算出两个日期之间相隔多少天的正确值。我觉得我已经很接近我想要的了,但对我来说,总是缺少一整天。

到目前为止我得到的是:

$start = new DateTimeImmutable('2018-07-31 12:30:00');
$end = new DateTimeImmutable('2018-08-28 00:00:00');
$diff = $start->diff($end);

$totalDays = (int) $diff->days; // 27
$hours = $diff->h;              // 11
$minutes = $diff->i;            // 30
$seconds = $diff->s;            //  0

$hoursAsDays = $hours / 24;               // 0.45833333333333
$minutesAsDays = $minutes / 60 / 24;      // 0.020833333333333
$secondsAsDays = $seconds / 60 / 60 / 24; // 0
$result = $totalDays + $hoursAsDays + $minutesAsDays + $secondsAsDays;

var_dump($result); // 27.479166666667

对我来说,从八月开始有整整 28 天。我以为我会得到类似的东西:28.45

编辑: 添加“2018-08-28”作为结束日期的用户将假定它会算作一整天。我如何才能安全地在我的计算中提及它?

您可以使用 strtotime() PHP 的函数简单地做到这一点:

$start = '2018-07-31 12:30:00';
$end = '2018-08-28 00:00:01';

echo $diffDays = (strtotime($end) - strtotime($start)) / 60 / 60 / 24;

结果:

27.479166666667

结果为真,因为在两个日期时间之间恰好有 27,5 天(按每天 24 小时计算):

27 days and early a half day:
from 2018-07-31 12:30 to 2018-07-31 23.59 = +1/2 day tot:    0,5
from 2018-08-01 00:00 to 2018-08-01 23.59 = +1 day   tot:    1,5
from 2018-08-02 00:00 to 2018-08-01 23.59 = +1 day   tot:    2,5
from 2018-08-03 00:00 to 2018-08-01 23.59 = +1 day   tot:    3,5
from 2018-08-04 00:00 to 2018-08-01 23.59 = +1 day   tot:    4,5
from 2018-08-05 00:00 to 2018-08-01 23.59 = +1 day   tot:    5,5
from 2018-08-06 00:00 to 2018-08-01 23.59 = +1 day   tot:    6,5
from 2018-08-07 00:00 to 2018-08-01 23.59 = +1 day   tot:    7,5
from 2018-08-08 00:00 to 2018-08-01 23.59 = +1 day   tot:    8,5
from 2018-08-09 00:00 to 2018-08-01 23.59 = +1 day   tot:    9,5
from 2018-08-10 00:00 to 2018-08-01 23.59 = +1 day   tot:   10,5
from 2018-08-11 00:00 to 2018-08-01 23.59 = +1 day   tot:   11,5
from 2018-08-12 00:00 to 2018-08-01 23.59 = +1 day   tot:   12,5
from 2018-08-13 00:00 to 2018-08-01 23.59 = +1 day   tot:   13,5
from 2018-08-14 00:00 to 2018-08-01 23.59 = +1 day   tot:   14,5
from 2018-08-15 00:00 to 2018-08-01 23.59 = +1 day   tot:   15,5
from 2018-08-16 00:00 to 2018-08-01 23.59 = +1 day   tot:   16,5
from 2018-08-17 00:00 to 2018-08-01 23.59 = +1 day   tot:   17,5
from 2018-08-18 00:00 to 2018-08-01 23.59 = +1 day   tot:   18,5
from 2018-08-19 00:00 to 2018-08-01 23.59 = +1 day   tot:   19,5
from 2018-08-20 00:00 to 2018-08-01 23.59 = +1 day   tot:   20,5
from 2018-08-21 00:00 to 2018-08-01 23.59 = +1 day   tot:   21,5
from 2018-08-22 00:00 to 2018-08-01 23.59 = +1 day   tot:   22,5
from 2018-08-23 00:00 to 2018-08-01 23.59 = +1 day   tot:   23,5
from 2018-08-24 00:00 to 2018-08-01 23.59 = +1 day   tot:   24,5
from 2018-08-25 00:00 to 2018-08-01 23.59 = +1 day   tot:   25,5
from 2018-08-26 00:00 to 2018-08-01 23.59 = +1 day   tot:   26,5
from 2018-08-27 00:00 to 2018-08-01 23.59 = +1 day   tot:   27,5
from 2018-08-28 00:00 to 2018-08-28 00:00 = +0 day   tot:   27,5

使用 strtotime(),您可以将 date/datetime 字符串转换为即时版本(从纪元开始计算,以秒为单位)。

关于您的编辑,考虑到计算中的结束日期,将其时间设置为23:59:59:

$end = date('Y-m-d 23:59:59', strtotime('2018-08-28 00:00:01'));

因此,结果将是:

28.479155092593

为了让这个完整,这是我现在的结果:

/**
 * @param DateTimeInterface $start
 * @param DateTimeInterface $end
 * @return float|int
 */
public static function getDaysBetweenDates(DateTimeInterface $start, DateTimeInterface $end)
{
    $start = new DateTimeImmutable("@{$start->getTimestamp()}");
    $end = new DateTimeImmutable("@{$end->getTimestamp()}");
    $diff = $start->diff($end);

    if (
        (int) $end->format('H') === 0
        && (int) $end->format('i') === 0
        && (int) $end->format('s') === 0
    ) {
        $end = new DateTimeImmutable($end->format('Y-m-d') . ' 23:59:59');
        $diff = $start->diff($end);
    }

    $totalDays = (int) $diff->days;
    $hours = $diff->h;
    $minutes = $diff->i;
    $seconds = $diff->s;

    $hoursAsDays = $hours / 24;
    $minutesAsDays = $minutes / 60 / 24;
    $secondsAsDays = $seconds / 60 / 60 / 24;
    return $totalDays + $hoursAsDays + $minutesAsDays + $secondsAsDays;
}

勾选DateInterval格式方法中的%a选项

$date1 = new DateTime('19-04-2019');
$date2 = new DateTime('5-05-2020');
echo $date2->diff($date1)->format('%a days');

输出:

382 days