Calculating how many "Midnights" are one past date in PHP?

I have a start / end time for the calculation that I am trying to do, and I have problems if the end time is before 12 am the day after the start. In addition, I need to calculate how many days the start time has passed.

What I have: Start date, End date

What I need: - How many "Midnights" is the end date after the start date?

Has anyone done something like this?

+3
source share
1 answer

PHP 5.3, , unix, . , , . , setTime(0,0), .

DateTime.

$start = new DateTime('2011-03-07 12:23:45');
$end = new DateTime('2011-03-08 1:23:45');

$start->setTime(0,0);
$end->setTime(0,0);

$midnights = $start->diff($end)->days;

setTime() 0, 24 . setTime() 1, 24 .

diff() 5.3 DateInterval. 5.2 DateTime, , Unix.

$midnights = ($end->format('U') - $start->format('U')) / 86400

abs() /, .

. , , , DST.

php date documentation 86400 ( ), , DST.

DateTimes UTC.

$utcTimezone = new DateTimeZone('UTC');
$start = new DateTime('2011-03-07 12:23:45', $utcTimezone);
$end = new DateTime('2011-03-08 1:23:45', $utcTimezone);
+4

Source: https://habr.com/ru/post/1796718/


All Articles