active_year = $date != null ? date('Y', strtotime($date)) : date('Y'); $this->active_month = $date != null ? date('m', strtotime($date)) : date('m'); $this->active_day = $date != null ? date('d', strtotime($date)) : date('d'); } public function add_event($date, $days = 1, $class = '') { $class = $class ? $class : $class; $this->events[] = [$date, $days, $class]; } public function __toString() { $num_days = date('t', strtotime($this->active_day . '-' . $this->active_month . '-' . $this->active_year)); $days = [0 => 'Sun', 1 => 'Mon', 2 => 'Tue', 3 => 'Wed', 4 => 'Thu', 5 => 'Fri', 6 => 'Sat']; $first_day_of_week = array_search(date('D', strtotime($this->active_year . '-' . $this->active_month . '-1')), $days); $html = ''; $week = 0; for ($i = $first_day_of_week; $i > 0; $i--) { $html .= ''; $week ++; } for ($i = 1; $i <= $num_days; $i++) { if ($week % 7 == 0) { $html .= ''; } $html .= ''; $eventDay = false; foreach ($this->events as $event) { for ($d = 0; $d <= ($event[1]-1); $d++) { if (date('y-m-d', strtotime($this->active_year . '-' . $this->active_month . '-' . $i . ' -' . $d . ' day')) == date('y-m-d', strtotime($event[0]))) { $html .= '' . $i . ''; $eventDay = true; } } if ($eventDay) { break; } } if (!$eventDay) { $html .= '' . $i . ''; } $html .= ''; $week ++; } if ($week % 7 > 0) { for ($i = 1; $i <= (7 - $week % 7); $i++) { $html .= ''; } } $html .= ''; return $html; } }