2
0

helpers_date.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. if (!function_exists('days_between')) {
  3. function days_between(DateTime $datetime1, DateTime $datetime2): int
  4. {
  5. $interval = $datetime1->diff($datetime2);
  6. return $interval->days;
  7. }
  8. }
  9. if (!function_exists('is_leap_year')) {
  10. function is_leap_year(DateTime $date): bool
  11. {
  12. $year = $date->format('Y');
  13. return ($year % 4 === 0 && $year % 100 !== 0) || ($year % 400 === 0);
  14. }
  15. }
  16. if (!function_exists('is_weekend')) {
  17. /**
  18. * @param DateTime $date
  19. * @return bool
  20. */
  21. function is_weekend(DateTime $date): bool
  22. {
  23. return in_array($date->format('N'), [6, 7]);
  24. }
  25. }
  26. if (!function_exists('is_today')) {
  27. /**
  28. * @param DateTime $date
  29. * @return bool
  30. */
  31. function is_today(DateTime $date): bool
  32. {
  33. return $date->format('Y-m-d') === (new DateTime())->format('Y-m-d');
  34. }
  35. }
  36. if (!function_exists('is_past')) {
  37. function is_past(DateTime $date): bool
  38. {
  39. return (new DateTime()) > $date;
  40. }
  41. }