Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. <?php
  2. /**
  3. * This function gets a calendar from the selected month of the selected year.
  4. * It's calculating the week number from the first and the last day of the month,
  5. * starting with the first day of the first week number of the current month
  6. * and ends with the last day of the last week number of the current month.
  7. *
  8. * @author Joel Bladt<joel@bladt.de>
  9. * @param int $month
  10. * @param null|int $year
  11. * @return array
  12. */
  13. function getCalendarOfMonth(int $month, int $year = null): array {
  14. $dt = new \DateTime();
  15.  
  16. $year = (int) (is_null($year)) ? $dt->format('Y') : $year;
  17. $last = (int) $dt->setDate($year, $month + 1, 0)->format('W');
  18. $first = (int) $dt->setDate($year, $month, 1)->format('W');
  19.  
  20. $first = ($first > $last) ? 0 : $first;
  21.  
  22. for (; $first <= $last; $first++) {
  23. $dt->setISODate($year, $first);
  24.  
  25. for ($i = 0; $i < 7; $i++) {
  26. if ($i > 0) {
  27. $dt->modify('+1 day');
  28. }
  29.  
  30. $result[] = $dt->format('d.m.Y');
  31. }
  32. }
  33.  
  34. return $result;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement