DateHelper.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Helpers;
  4. use Carbon\Exceptions\InvalidDateException;
  5. use DateTime;
  6. use Exception;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Str;
  9. class DateHelper
  10. {
  11. const DB_FORMAT = 'Y-MM-DD';
  12. const HUMAN_TEXT_FORMAT = 'D MMMM Y';
  13. const HUMAN_DATE_FORMAT = 'DD.MM.Y';
  14. const MINIMAL_RANDOM_YEAR = 1970;
  15. const SECONDS_IN_YEAR = 365 * 24 * 60 * 60;
  16. const YEARS = 'год|года|лет';
  17. const MONTHS = 'месяц|месяца|месяцев';
  18. const DAYS = 'день|дня|дней';
  19. /**
  20. * @param string $date
  21. * @param bool $monthAsNumber
  22. * @return string
  23. */
  24. public static function getHumanDate(string $date, bool $monthAsNumber = false): string
  25. {
  26. $format = ($monthAsNumber) ? self::HUMAN_DATE_FORMAT : self::HUMAN_TEXT_FORMAT;
  27. $dt = Carbon::parse($date);
  28. return Str::lower($dt->isoFormat($format));
  29. }
  30. public static function getHumanDayOfWeek(string $date): string
  31. {
  32. $dt = Carbon::parse($date);
  33. return Str::ucfirst($dt->dayName);
  34. }
  35. /**
  36. * Случайная дата за последние X лет в формате YYYY-MM-DD
  37. * @param int|null $last_years
  38. * @return string
  39. */
  40. public static function getRandomDate(?int $last_years = 7): string
  41. {
  42. if (($last_years > ((int)date('Y') - self::MINIMAL_RANDOM_YEAR)) || ($last_years < 0)) {
  43. throw new InvalidDateException('last_years', $last_years, 1);
  44. }
  45. $seconds = rand(0, $last_years * self::SECONDS_IN_YEAR); // случайное число секунд в диапазоне от 0 до Х лет
  46. return Carbon::createFromTimestamp(strtotime('now') - $seconds)->isoFormat(self::DB_FORMAT);
  47. }
  48. /**
  49. * @param string $date
  50. * @return string
  51. */
  52. public static function getDateForDB(string $date): string
  53. {
  54. return Carbon::parse($date)->isoFormat(self::DB_FORMAT);
  55. }
  56. /**
  57. * @param string $date
  58. * @param int $months
  59. * @return string
  60. */
  61. public static function addMonths(string $date, int $months): string
  62. {
  63. return Carbon::parse($date)->addMonths($months)->isoFormat(self::DB_FORMAT);
  64. }
  65. /**
  66. * @param string $string
  67. * @return bool
  68. */
  69. public static function isDate(string $string): bool
  70. {
  71. if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$string)) {
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. public static function getDateOfWeek(int $year, int $weekNumber, $dayOfWeek = 1): string
  78. {
  79. $date = new DateTime();
  80. // Set the date to the specified year and week number, with Monday as the day (1)
  81. $date->setISODate($year, $weekNumber, $dayOfWeek);
  82. return $date->format('Y-m-d');
  83. }
  84. public static function excelDateToISODate(int $excelDate): string
  85. {
  86. $ts = ($excelDate - 25569) * 86400;
  87. $date = new DateTime();
  88. $date->setTimestamp($ts);
  89. return $date->format('Y-m-d');
  90. }
  91. }