Last active
May 7, 2019 18:02
-
-
Save jonataa/babc14ff48e7f5f3b1ecda33f023f578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function countExtraNightBetweenDates(DateTime $begin, DateTime $end, array $extraNightHours): int | |
{ | |
$interval = DateInterval::createFromDateString('1 hour'); | |
$period = new DatePeriod($begin, $interval, $end); | |
$extraNightTotal = 0; | |
foreach($period as $dt) { | |
if (in_array((int) $dt->format('H'), $extraNightHours)) { | |
$extraNightTotal++; | |
} | |
} | |
return $extraNightTotal ? ($extraNightTotal - 1) : $extraNightTotal; | |
} | |
$extraNightHours = [22, 23, 0, 1, 2, 3, 4, 5]; | |
$begin = new DateTime('2019-05-07 23:45:00'); | |
$end = new DateTime('2019-05-08 04:30:00'); | |
$extraNightTotal = countExtraNightBetweenDates($begin, $end, $extraNightHours); | |
var_dump($extraNightTotal); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment