Date.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
  3. use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
  4. use PhpOffice\PhpSpreadsheet\Calculation\Exception;
  5. use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
  6. use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
  7. use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
  8. class Date
  9. {
  10. use ArrayEnabled;
  11. /**
  12. * DATE.
  13. *
  14. * The DATE function returns a value that represents a particular date.
  15. *
  16. * NOTE: When used in a Cell Formula, MS Excel changes the cell format so that it matches the date
  17. * format of your regional settings. PhpSpreadsheet does not change cell formatting in this way.
  18. *
  19. * Excel Function:
  20. * DATE(year,month,day)
  21. *
  22. * PhpSpreadsheet is a lot more forgiving than MS Excel when passing non numeric values to this function.
  23. * A Month name or abbreviation (English only at this point) such as 'January' or 'Jan' will still be accepted,
  24. * as will a day value with a suffix (e.g. '21st' rather than simply 21); again only English language.
  25. *
  26. * @param array|int $year The value of the year argument can include one to four digits.
  27. * Excel interprets the year argument according to the configured
  28. * date system: 1900 or 1904.
  29. * If year is between 0 (zero) and 1899 (inclusive), Excel adds that
  30. * value to 1900 to calculate the year. For example, DATE(108,1,2)
  31. * returns January 2, 2008 (1900+108).
  32. * If year is between 1900 and 9999 (inclusive), Excel uses that
  33. * value as the year. For example, DATE(2008,1,2) returns January 2,
  34. * 2008.
  35. * If year is less than 0 or is 10000 or greater, Excel returns the
  36. * #NUM! error value.
  37. * @param array|int $month A positive or negative integer representing the month of the year
  38. * from 1 to 12 (January to December).
  39. * If month is greater than 12, month adds that number of months to
  40. * the first month in the year specified. For example, DATE(2008,14,2)
  41. * returns the serial number representing February 2, 2009.
  42. * If month is less than 1, month subtracts the magnitude of that
  43. * number of months, plus 1, from the first month in the year
  44. * specified. For example, DATE(2008,-3,2) returns the serial number
  45. * representing September 2, 2007.
  46. * @param array|int $day A positive or negative integer representing the day of the month
  47. * from 1 to 31.
  48. * If day is greater than the number of days in the month specified,
  49. * day adds that number of days to the first day in the month. For
  50. * example, DATE(2008,1,35) returns the serial number representing
  51. * February 4, 2008.
  52. * If day is less than 1, day subtracts the magnitude that number of
  53. * days, plus one, from the first day of the month specified. For
  54. * example, DATE(2008,1,-15) returns the serial number representing
  55. * December 16, 2007.
  56. *
  57. * @return mixed Excel date/time serial value, PHP date/time serial value or PHP date/time object,
  58. * depending on the value of the ReturnDateType flag
  59. * If an array of numbers is passed as the argument, then the returned result will also be an array
  60. * with the same dimensions
  61. */
  62. public static function fromYMD($year, $month, $day)
  63. {
  64. if (is_array($year) || is_array($month) || is_array($day)) {
  65. return self::evaluateArrayArguments([self::class, __FUNCTION__], $year, $month, $day);
  66. }
  67. $baseYear = SharedDateHelper::getExcelCalendar();
  68. try {
  69. $year = self::getYear($year, $baseYear);
  70. $month = self::getMonth($month);
  71. $day = self::getDay($day);
  72. self::adjustYearMonth($year, $month, $baseYear);
  73. } catch (Exception $e) {
  74. return $e->getMessage();
  75. }
  76. // Execute function
  77. $excelDateValue = SharedDateHelper::formattedPHPToExcel($year, $month, $day);
  78. return Helpers::returnIn3FormatsFloat($excelDateValue);
  79. }
  80. /**
  81. * Convert year from multiple formats to int.
  82. *
  83. * @param mixed $year
  84. */
  85. private static function getYear($year, int $baseYear): int
  86. {
  87. $year = ($year !== null) ? StringHelper::testStringAsNumeric((string) $year) : 0;
  88. if (!is_numeric($year)) {
  89. throw new Exception(ExcelError::VALUE());
  90. }
  91. $year = (int) $year;
  92. if ($year < ($baseYear - 1900)) {
  93. throw new Exception(ExcelError::NAN());
  94. }
  95. if ((($baseYear - 1900) !== 0) && ($year < $baseYear) && ($year >= 1900)) {
  96. throw new Exception(ExcelError::NAN());
  97. }
  98. if (($year < $baseYear) && ($year >= ($baseYear - 1900))) {
  99. $year += 1900;
  100. }
  101. return (int) $year;
  102. }
  103. /**
  104. * Convert month from multiple formats to int.
  105. *
  106. * @param mixed $month
  107. */
  108. private static function getMonth($month): int
  109. {
  110. if (($month !== null) && (!is_numeric($month))) {
  111. $month = SharedDateHelper::monthStringToNumber($month);
  112. }
  113. $month = ($month !== null) ? StringHelper::testStringAsNumeric((string) $month) : 0;
  114. if (!is_numeric($month)) {
  115. throw new Exception(ExcelError::VALUE());
  116. }
  117. return (int) $month;
  118. }
  119. /**
  120. * Convert day from multiple formats to int.
  121. *
  122. * @param mixed $day
  123. */
  124. private static function getDay($day): int
  125. {
  126. if (($day !== null) && (!is_numeric($day))) {
  127. $day = SharedDateHelper::dayStringToNumber($day);
  128. }
  129. $day = ($day !== null) ? StringHelper::testStringAsNumeric((string) $day) : 0;
  130. if (!is_numeric($day)) {
  131. throw new Exception(ExcelError::VALUE());
  132. }
  133. return (int) $day;
  134. }
  135. private static function adjustYearMonth(int &$year, int &$month, int $baseYear): void
  136. {
  137. if ($month < 1) {
  138. // Handle year/month adjustment if month < 1
  139. --$month;
  140. $year += ceil($month / 12) - 1;
  141. $month = 13 - abs($month % 12);
  142. } elseif ($month > 12) {
  143. // Handle year/month adjustment if month > 12
  144. $year += floor($month / 12);
  145. $month = ($month % 12);
  146. }
  147. // Re-validate the year parameter after adjustments
  148. if (($year < $baseYear) || ($year >= 10000)) {
  149. throw new Exception(ExcelError::NAN());
  150. }
  151. }
  152. }