DateInterval общее количество дней с плавающей точкой

На данный момент мне нет дела до часовых поясов, я просто хочу иметь правильное рассчитанное значение количества дней между двумя датами. Я думаю, что я довольно близок к тому, что я хочу, но для меня всегда хватает полного дня.

Вот что я получил так далеко:

$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» в качестве даты окончания, будут считать, что она будет считаться полным днем. Как я могу безопасно сделать это возможным, чтобы быть упомянутым в моем расчете?

1

Решение

Вы можете просто сделать это с 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 () Вы можете преобразовать строку даты / даты / времени в мгновенную версию (отсчитать от эпохи в секундах).

Что касается вашего редактирования, чтобы рассмотреть день окончания в расчете, установите его время как 23:59:59:

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

Таким образом, результат будет:

28.479155092593
1

Другие решения

Просто чтобы закончить, вот мой результат:

/**
* @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;
}
1

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector