ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Days360.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
9 class Days360
10 {
39  public static function between($startDate = 0, $endDate = 0, $method = false)
40  {
41  try {
42  $startDate = Helpers::getDateValue($startDate);
43  $endDate = Helpers::getDateValue($endDate);
44  } catch (Exception $e) {
45  return $e->getMessage();
46  }
47 
48  if (!is_bool($method)) {
49  return Functions::VALUE();
50  }
51 
52  // Execute function
53  $PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate);
54  $startDay = $PHPStartDateObject->format('j');
55  $startMonth = $PHPStartDateObject->format('n');
56  $startYear = $PHPStartDateObject->format('Y');
57 
58  $PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate);
59  $endDay = $PHPEndDateObject->format('j');
60  $endMonth = $PHPEndDateObject->format('n');
61  $endYear = $PHPEndDateObject->format('Y');
62 
63  return self::dateDiff360((int) $startDay, (int) $startMonth, (int) $startYear, (int) $endDay, (int) $endMonth, (int) $endYear, !$method);
64  }
65 
69  private static function dateDiff360(int $startDay, int $startMonth, int $startYear, int $endDay, int $endMonth, int $endYear, bool $methodUS): int
70  {
71  $startDay = self::getStartDay($startDay, $startMonth, $startYear, $methodUS);
72  $endDay = self::getEndDay($endDay, $endMonth, $endYear, $startDay, $methodUS);
73 
74  return $endDay + $endMonth * 30 + $endYear * 360 - $startDay - $startMonth * 30 - $startYear * 360;
75  }
76 
77  private static function getStartDay(int $startDay, int $startMonth, int $startYear, bool $methodUS): int
78  {
79  if ($startDay == 31) {
80  --$startDay;
81  } elseif ($methodUS && ($startMonth == 2 && ($startDay == 29 || ($startDay == 28 && !Helpers::isLeapYear($startYear))))) {
82  $startDay = 30;
83  }
84 
85  return $startDay;
86  }
87 
88  private static function getEndDay(int $endDay, int &$endMonth, int &$endYear, int $startDay, bool $methodUS): int
89  {
90  if ($endDay == 31) {
91  if ($methodUS && $startDay != 30) {
92  $endDay = 1;
93  if ($endMonth == 12) {
94  ++$endYear;
95  $endMonth = 1;
96  } else {
97  ++$endMonth;
98  }
99  } else {
100  $endDay = 30;
101  }
102  }
103 
104  return $endDay;
105  }
106 }
static getStartDay(int $startDay, int $startMonth, int $startYear, bool $methodUS)
Definition: Days360.php:77
static getEndDay(int $endDay, int &$endMonth, int &$endYear, int $startDay, bool $methodUS)
Definition: Days360.php:88
static isLeapYear($year)
Identify if a year is a leap year or not.
Definition: Helpers.php:19
static getDateValue($dateValue, bool $allowBool=true)
getDateValue.
Definition: Helpers.php:31
static between($startDate=0, $endDate=0, $method=false)
DAYS360.
Definition: Days360.php:39
static dateDiff360(int $startDay, int $startMonth, int $startYear, int $endDay, int $endMonth, int $endYear, bool $methodUS)
Return the number of days between two dates based on a 360 day calendar.
Definition: Days360.php:69