diff --git a/src/applications/calendar/query/PhabricatorCalendarEventSearchEngine.php b/src/applications/calendar/query/PhabricatorCalendarEventSearchEngine.php index 0c9e13b03f..c8a788adb8 100644 --- a/src/applications/calendar/query/PhabricatorCalendarEventSearchEngine.php +++ b/src/applications/calendar/query/PhabricatorCalendarEventSearchEngine.php @@ -76,11 +76,33 @@ final class PhabricatorCalendarEventSearchEngine $display_start = $start_day->format('U'); $display_end = $next->format('U'); + // 0 = Sunday is always the start of the week, for now + $start_of_week = 0; + $end_of_week = 6 - $start_of_week; + + $first_of_month = $start_day->format('w'); + $last_of_month = id(clone $next)->modify('-1 day')->format('w'); + if (!$min_range || ($min_range < $display_start)) { $min_range = $display_start; + + if ($this->isMonthView($saved) && + $first_of_month > $start_of_week) { + $min_range = id(clone $start_day) + ->modify('-'.$first_of_month.' days') + ->format('U'); + } } if (!$max_range || ($max_range > $display_end)) { $max_range = $display_end; + + if ($this->isMonthView($saved) && + $last_of_month < $end_of_week) { + $max_range = id(clone $next) + ->modify('+'.(6 - $first_of_month).' days') + ->format('U'); + } + } } diff --git a/src/view/phui/calendar/PHUICalendarMonthView.php b/src/view/phui/calendar/PHUICalendarMonthView.php index 5e74af0ff8..7eb35f76a5 100644 --- a/src/view/phui/calendar/PHUICalendarMonthView.php +++ b/src/view/phui/calendar/PHUICalendarMonthView.php @@ -56,14 +56,8 @@ final class PHUICalendarMonthView extends AphrontView { } $events = msort($this->events, 'getEpochStart'); - $days = $this->getDatesInMonth(); - require_celerity_resource('phui-calendar-month-css'); - - $first = reset($days); - $empty = $first->format('w'); - $cell_lists = array(); $empty_cell = array( 'list' => null, @@ -73,6 +67,13 @@ final class PHUICalendarMonthView extends AphrontView { 'class' => null, ); + require_celerity_resource('phui-calendar-month-css'); + + $first = reset($days); + $start_of_week = 0; + + $empty = $first->format('w'); + for ($ii = 0; $ii < $empty; $ii++) { $cell_lists[] = $empty_cell; } @@ -409,22 +410,41 @@ final class PHUICalendarMonthView extends AphrontView { $month = $this->month; $year = $this->year; - // Get the year and month numbers of the following month, so we can - // determine when this month ends. list($next_year, $next_month) = $this->getNextYearAndMonth(); - $end_date = new DateTime("{$next_year}-{$next_month}-01", $timezone); - $end_epoch = $end_date->format('U'); + + $start_of_week = 0; + $end_of_week = 6 - $start_of_week; + $days_in_month = id(clone $end_date)->modify('-1 day')->format('d'); + + $first_month_day_date = new DateTime("{$year}-{$month}-01", $timezone); + $last_month_day_date = id(clone $end_date)->modify('-1 day'); + + $first_weekday_of_month = $first_month_day_date->format('w'); + $last_weekday_of_month = $last_month_day_date->format('w'); + + $num_days_display = $days_in_month; + if ($start_of_week < $first_weekday_of_month) { + $num_days_display += $first_weekday_of_month; + } + if ($end_of_week > $last_weekday_of_month) { + $num_days_display += (6 - $last_weekday_of_month); + $end_date->modify('+'.(6 - $last_weekday_of_month).' days'); + } $days = array(); - for ($day = 1; $day <= 31; $day++) { - $day_date = new DateTime("{$year}-{$month}-{$day}", $timezone); + $day_date = id(clone $first_month_day_date) + ->modify('-'.$first_weekday_of_month.' days'); + + for ($day = 1; $day <= $num_days_display; $day++) { $day_epoch = $day_date->format('U'); + $end_epoch = $end_date->format('U'); if ($day_epoch >= $end_epoch) { break; } else { - $days[] = $day_date; + $days[] = clone $day_date; } + $day_date->modify('+1 day'); } return $days;