Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * This function gets a calendar from the selected month of the selected year.
- * It's calculating the week number from the first and the last day of the month,
- * starting with the first day of the first week number of the current month
- * and ends with the last day of the last week number of the current month.
- *
- * @author Joel Bladt<joel@bladt.de>
- * @param int $month
- * @param null|int $year
- * @return array
- */
- function getCalendarOfMonth(int $month, int $year = null): array {
- $dt = new \DateTime();
- $year = (int) (is_null($year)) ? $dt->format('Y') : $year;
- $last = (int) $dt->setDate($year, $month + 1, 0)->format('W');
- $first = (int) $dt->setDate($year, $month, 1)->format('W');
- $first = ($first > $last) ? 0 : $first;
- for (; $first <= $last; $first++) {
- $dt->setISODate($year, $first);
- for ($i = 0; $i < 7; $i++) {
- if ($i > 0) {
- $dt->modify('+1 day');
- }
- $result[] = $dt->format('d.m.Y');
- }
- }
- return $result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement