DateParts.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Functions;
  6. use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
  7. class DateParts
  8. {
  9. use ArrayEnabled;
  10. /**
  11. * DAYOFMONTH.
  12. *
  13. * Returns the day of the month, for a specified date. The day is given as an integer
  14. * ranging from 1 to 31.
  15. *
  16. * Excel Function:
  17. * DAY(dateValue)
  18. *
  19. * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  20. * PHP DateTime object, or a standard date string
  21. * Or can be an array of date values
  22. *
  23. * @return array|int|string Day of the month
  24. * If an array of numbers is passed as the argument, then the returned result will also be an array
  25. * with the same dimensions
  26. */
  27. public static function day($dateValue)
  28. {
  29. if (is_array($dateValue)) {
  30. return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
  31. }
  32. $weirdResult = self::weirdCondition($dateValue);
  33. if ($weirdResult >= 0) {
  34. return $weirdResult;
  35. }
  36. try {
  37. $dateValue = Helpers::getDateValue($dateValue);
  38. } catch (Exception $e) {
  39. return $e->getMessage();
  40. }
  41. // Execute function
  42. $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
  43. return (int) $PHPDateObject->format('j');
  44. }
  45. /**
  46. * MONTHOFYEAR.
  47. *
  48. * Returns the month of a date represented by a serial number.
  49. * The month is given as an integer, ranging from 1 (January) to 12 (December).
  50. *
  51. * Excel Function:
  52. * MONTH(dateValue)
  53. *
  54. * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  55. * PHP DateTime object, or a standard date string
  56. * Or can be an array of date values
  57. *
  58. * @return array|int|string Month of the year
  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 month($dateValue)
  63. {
  64. if (is_array($dateValue)) {
  65. return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
  66. }
  67. try {
  68. $dateValue = Helpers::getDateValue($dateValue);
  69. } catch (Exception $e) {
  70. return $e->getMessage();
  71. }
  72. if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
  73. return 1;
  74. }
  75. // Execute function
  76. $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
  77. return (int) $PHPDateObject->format('n');
  78. }
  79. /**
  80. * YEAR.
  81. *
  82. * Returns the year corresponding to a date.
  83. * The year is returned as an integer in the range 1900-9999.
  84. *
  85. * Excel Function:
  86. * YEAR(dateValue)
  87. *
  88. * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  89. * PHP DateTime object, or a standard date string
  90. * Or can be an array of date values
  91. *
  92. * @return array|int|string Year
  93. * If an array of numbers is passed as the argument, then the returned result will also be an array
  94. * with the same dimensions
  95. */
  96. public static function year($dateValue)
  97. {
  98. if (is_array($dateValue)) {
  99. return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
  100. }
  101. try {
  102. $dateValue = Helpers::getDateValue($dateValue);
  103. } catch (Exception $e) {
  104. return $e->getMessage();
  105. }
  106. if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
  107. return 1900;
  108. }
  109. // Execute function
  110. $PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
  111. return (int) $PHPDateObject->format('Y');
  112. }
  113. /**
  114. * @param mixed $dateValue Excel date serial value (float), PHP date timestamp (integer),
  115. * PHP DateTime object, or a standard date string
  116. */
  117. private static function weirdCondition($dateValue): int
  118. {
  119. // Excel does not treat 0 consistently for DAY vs. (MONTH or YEAR)
  120. if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900 && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
  121. if (is_bool($dateValue)) {
  122. return (int) $dateValue;
  123. }
  124. if ($dateValue === null) {
  125. return 0;
  126. }
  127. if (is_numeric($dateValue) && $dateValue < 1 && $dateValue >= 0) {
  128. return 0;
  129. }
  130. }
  131. return -1;
  132. }
  133. }