Last active
August 28, 2017 00:33
-
-
Save Pathologic/afab754f6b772a8844c0a4cff1256ec0 to your computer and use it in GitHub Desktop.
php calendar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Calendar | |
*/ | |
abstract class Calendar | |
{ | |
protected $month = 0; | |
protected $year = 0; | |
protected $dates = array(); | |
protected $firstDay = 1; | |
protected $config = array( | |
'activeClass' => 'active', | |
'otherMonthClass' => 'other-month', | |
'holidayClass' => 'holiday' | |
); | |
protected $firstCalendarDate = ''; | |
protected $lastCalendarDate = ''; | |
/** | |
* @param array $cfg | |
* @return $this | |
*/ | |
public function setConfig($cfg = array()) | |
{ | |
$this->config = array_merge($this->config, $cfg); | |
return $this; | |
} | |
/** | |
* @param string $month | |
* @return $this | |
*/ | |
public function month($month = '') | |
{ | |
if (empty($month)) { | |
$month = date('m'); | |
} else { | |
$month = (int)$month; | |
if (!$month) { | |
$month = 1; | |
} | |
if ($month > 12) { | |
$month = 12; | |
} | |
} | |
$this->month = $month; | |
return $this; | |
} | |
/** | |
* @param string $year | |
* @return $this | |
*/ | |
public function year($year = '') | |
{ | |
if (empty($year)) { | |
$year = date('Y'); | |
} else { | |
$year = (int)$year; | |
} | |
$this->year = $year; | |
return $this; | |
} | |
/** | |
* return string; | |
*/ | |
public function setDates() | |
{ | |
$ts = mktime(0, 0, 0, $this->month, 1, $this->year); | |
$startDayOfWeek = date('N', $ts); | |
$startDay = ($this->firstDay == 1 ? 2 : 1) - $startDayOfWeek; | |
$finishDay = $startDay + 42; | |
$this->firstCalendarDate = mktime(0, 0, 0, $this->month, $startDay, $this->year); | |
$this->lastCalendarDate = mktime(0, 0, 0, $this->month, $finishDay - 1, $this->year); | |
while ($startDay < $finishDay) { | |
$ts = mktime(0, 0, 0, $this->month, $startDay, $this->year); | |
$this->dates[] = array( | |
'day' => date('N', $ts), | |
'date' => date('d', $ts), | |
'month' => date('m', $ts), | |
'year' => date('Y', $ts) | |
); | |
$startDay++; | |
} | |
return $this; | |
} | |
/** | |
* @param string $format | |
* @return int|string | |
*/ | |
public function getFirstCalendarDate($format = '') | |
{ | |
return empty($format) ? $this->firstCalendarDate : date($format, $this->firstCalendarDate); | |
} | |
/** | |
* @param string $format | |
* @return int|string | |
*/ | |
public function getLastCalendarDate($format = '') | |
{ | |
return empty($format) ? $this->lastCalendarDate : date($format, $this->lastCalendarDate); | |
} | |
/** | |
* @return string | |
*/ | |
public function render() | |
{ | |
$this->setDates(); | |
$out = ''; | |
$i = 0; | |
for ($w = 1; $w <= 6; $w++) { | |
$week = ''; | |
for ($d = 1; $d <= 7; $d++) { | |
$week .= $this->renderDay($this->dates[$i]); | |
$i++; | |
} | |
$out .= $this->renderWeek($week); | |
} | |
return $this->renderOuter($out); | |
} | |
/** | |
* @param string $wrap | |
* @return string | |
*/ | |
abstract protected function renderOuter($wrap = ''); | |
/** | |
* @param string $week | |
* @return string | |
*/ | |
abstract protected function renderWeek($week = ''); | |
/** | |
* @param array $data | |
* @return string | |
*/ | |
abstract protected function renderDay($data = array()); | |
/** | |
* @param array $data | |
* @return array | |
*/ | |
public function getClasses(array $data) | |
{ | |
$out = array(); | |
$currentDate = date('d.m.Y'); | |
if ($currentDate === $data['date'] . '.' . $data['month'] . '.' . $data['year']) { | |
$out['activeClass'] = isset($this->config['activeClass']) ? $this->config['activeClass'] : 'active'; | |
} | |
if ($data['month'] != $this->month) { | |
$out['otherMonthClass'] = isset($this->config['otherMonthClass']) ? $this->config['otherMonthClass'] : 'other-month'; | |
} | |
if ($data['day'] == 6 || $data['day'] == 7) { | |
$out['holidayClass'] = isset($this->config['holidayClass']) ? $this->config['holidayClass'] : 'holiday'; | |
} | |
$out['classnames'] = implode(' ', array_values($out)); | |
$out['classes'] = ' class="' . $out['classnames'] . '"'; | |
return $out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment