Yasumi\Yasumi::nextWorkingDay PHP Method

nextWorkingDay() public static method

public static nextWorkingDay ( string $class, $startDate, $workingDays = 1 ) : DateTime
$class string holiday provider name
$startDate DateTime Start date, defaults to today
$workingDays int
return DateTime
    public static function nextWorkingDay($class, $startDate, $workingDays = 1)
    {
        /* @TODO we should accept a timezone so we can accept int/string for $startDate */
        if (!$startDate instanceof DateTime) {
            throw new Exception('Bad parameter, DateTime expected');
        }
        // Setup start date, if its an instance of \DateTime, clone to prevent modification to original
        $date = $startDate instanceof DateTime ? clone $startDate : new DateTime($startDate);
        $provider = false;
        while ($workingDays > 0) {
            $date->add(new DateInterval('P1D'));
            if (!$provider || $provider->getYear() != $date->format('Y')) {
                $provider = self::create($class, $date->format('Y'));
            }
            if ($provider->isWorkingDay($date)) {
                $workingDays--;
            }
        }
        return $date;
    }

Usage Example

Beispiel #1
0
 public function testYearBoundary()
 {
     $startDate = new \DateTime('2015-12-20', new \DateTimeZone('America/New_York'));
     $result = Yasumi::nextWorkingDay('USA', $startDate, 20);
     /**
      * 20 working days between 20th Dec and 20th Jan
      * 2015-12-20 is a Sunday
      * 21st - 24th (4 Workdays)
      * 25th Christmas, 26th-27th Weekend
      * 28th - 31st (4 Workdays)
      * 1st Jan New Years, 2nd-3rd Weekend
      * 4th - 8th (5 Workdays)
      * 9th-10th Weekend
      * 11th-15th (5 Workdays)
      * 16th-17th Weekend, 18th Martin Luther King Day
      * 19th-20th (2 Workdays)
      *
      * @see https://www.timeanddate.com/calendar/?year=2016&country=1
      */
     $this->assertEquals('2016-01-20', $result->format('Y-m-d'));
     $startDate = new \DateTime('2016-01-20', new \DateTimeZone('America/New_York'));
     $result = Yasumi::prevWorkingDay('USA', $startDate, 20);
     $this->assertEquals('2015-12-18', $result->format('Y-m-d'));
 }