bebeboutik/modules/privatesales_delay/BusinessDaysCalculator.php

77 lines
2.6 KiB
PHP
Raw Normal View History

2016-01-04 12:49:26 +01:00
<?php
if(!class_exists('BusinessDaysCalculator')) {
class BusinessDaysCalculator
{
const MONDAY = 1;
const TUESDAY = 2;
const WEDNESDAY = 3;
const THURSDAY = 4;
const FRIDAY = 5;
const SATURDAY = 6;
const SUNDAY = 7;
/**
* @param DateTime $startDate Date to start calculations from
* @param DateTime[] $holidays Array of holidays, holidays are no conisdered business days.
* @param int[] $nonBusinessDays Array of days of the week which are not business days.
*/
public function __construct(DateTime $startDate, array $holidays, array $nonBusinessDays) {
$this->date = $startDate;
$this->holidays = $holidays;
$this->nonBusinessDays = $nonBusinessDays;
$this->assocHollyday();
}
public function assocHollyday()
{
$this->holidays[] = new DateTime((date('Y')+1)."-01-01");
$this->holidays[] = new DateTime(date('Y')."-01-01");
$this->holidays[] = new DateTime(date('Y')."-05-01");
$this->holidays[] = new DateTime(date('Y')."-05-08");
$this->holidays[] = new DateTime(date('Y')."-07-14");
$this->holidays[] = new DateTime(date('Y')."-08-15");
$this->holidays[] = new DateTime(date('Y')."-11-11");
$this->holidays[] = new DateTime(date('Y')."-12-25");
}
public function addBusinessDays($howManyDays) {
$i = 0;
while ($i < $howManyDays) {
$this->date->modify("+1 day");
if ($this->isBusinessDay($this->date)) {
$i++;
}
}
}
public function addDaysWithCheckLastDay($howManyDays) {
$i = 0;
while ($i < $howManyDays) {
$this->date->modify("+1 day");
$i++;
}
if (!$this->isBusinessDay($this->date)) {
$this->date->modify("+1 day");
}
}
2016-01-04 12:49:26 +01:00
public function getDate() {
return $this->date;
}
private function isBusinessDay(DateTime $date) {
if (in_array((int)$date->format('N'), $this->nonBusinessDays)) {
return false; //Date is a nonBusinessDay.
}
foreach ($this->holidays as $day) {
if ($date->format('Y-m-d') == $day->format('Y-m-d')) {
return false; //Date is a holiday.
}
}
return true; //Date is a business day.
}
}
}