Skip to content

Instantly share code, notes, and snippets.

@murilozilli
Last active October 23, 2020 08:53
Show Gist options
  • Save murilozilli/90e127fc2b0f905507e635d07cc487d4 to your computer and use it in GitHub Desktop.
Save murilozilli/90e127fc2b0f905507e635d07cc487d4 to your computer and use it in GitHub Desktop.
Get a number of business days after date
<?php
/**
* Created by PhpStorm.
* User: murilo
* Date: 23/10/20
* Time: 5:53 PM
*/
class DateHelper
{
//change at will
const HOLIDAY_DATES = [
['day' => 25, 'month' => 12],//christimas
['day' => 1, 'month' => 1],//new year
['day' => 13, 'month' => 4]//easter
];
/**
* @param int $numBusinessDays
* @param \DateTimeInterface $date
* @return \DateTime
*/
public static function getFutureBusinessDay(int $numBusinessDays, \DateTimeInterface $date)
{
$numBusinessDays = min($numBusinessDays, 1000);
$businessDayCount = 0;
$currentTimestamp = strtotime($date->format('Y-m-d'));
$holidayDates = self::getHolidayDates();
while ($businessDayCount < $numBusinessDays) {
$next1WD = strtotime('+1 weekday', $currentTimestamp);
$next1WDDate = date('Y-m-d', $next1WD);
if (!in_array($next1WDDate, $holidayDates)) {
$businessDayCount++;
}
$currentTimestamp = $next1WD;
}
return (new \DateTime())->setTimestamp($currentTimestamp);
}
/**
* @return array
*/
private static function getHolidayDates()
{
$holidays = [];
foreach (self::HOLIDAY_DATES as $holidayDate) {
$date = new \DateTime();
$date->setDate($date->format('Y'), $holidayDate['month'], $holidayDate['day']);
$holidays[] = $date->format('Y-m-d');
}
return $holidays;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment