Last active
August 29, 2015 14:03
-
-
Save borantula/d3562224fcadc53bfad9 to your computer and use it in GitHub Desktop.
Base Class for Making A Calendar in PHP
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 | |
//if you use it in a namespace, you'll need this line | |
//use \DateTime as DateTime; | |
//TODO: needs documentation and testing | |
/** | |
* Takes month and year, returns days of the month, next and prev month etc | |
* its used like $calendar = new CalendarBase(8,2014); | |
*/ | |
class CalendarBase { | |
public $month; | |
public $year; | |
//local month name | |
public $monthName; | |
/* | |
* DateTime object of given month | |
*/ | |
public $date; | |
public $dateObj; | |
/* | |
* How many days in this year | |
*/ | |
public $dayCount; | |
public $startingDay; | |
public $dayOffset; | |
/** | |
* Next month in array, year&month parameters | |
*/ | |
public $nextMonth = array(); | |
/** | |
* Prev month in array, first param month, next param year | |
*/ | |
public $prevMonth = array(); | |
public static $calendar = CAL_GREGORIAN; | |
public function __construct($month,$year) | |
{ | |
$this->month = $month; | |
$this->year = $year; | |
//try to build datetime obj | |
try { | |
$date = new DateTime($this->year.'-'.$this->month); | |
} catch (Exception $e) { | |
echo $e->getMessage(); | |
exit(1); | |
} | |
$this->dateObj = $date; | |
$this->date = $date->format('Y-m'); | |
//next prev months | |
$this->nextMonth(); | |
$this->prevMonth(); | |
$this->dayCount(); | |
$this->startingDay(); | |
$this->monthName = static::monthName($this->date); | |
} | |
private function dayCount() | |
{ | |
$this->dayCount = cal_days_in_month(static::$calendar,$this->month,$this->year); | |
} | |
private function startingDay() | |
{ | |
$this->startingDay = $this->dateObj->format('N'); | |
$this->dayOffset = $this->startingDay - 1; | |
} | |
private function nextMonth() | |
{ | |
$this->nextMonth['year'] = date('Y',strtotime('+1 month',strtotime($this->date))); | |
$this->nextMonth['month'] = date('m',strtotime('+1 month',strtotime($this->date))); | |
$this->nextMonth['daycount'] = cal_days_in_month(static::$calendar,$this->nextMonth['month'],$this->nextMonth['year']); | |
$this->nextMonth['month-name'] = static::monthName($this->nextMonth['year'].'-'.$this->nextMonth['month']); | |
} | |
private function prevMonth() | |
{ | |
$this->prevMonth['year'] = date('Y',strtotime('-1 month',strtotime($this->date))); | |
$this->prevMonth['month'] = date('m',strtotime('-1 month',strtotime($this->date))); | |
$this->prevMonth['daycount'] = cal_days_in_month(static::$calendar,$this->prevMonth['month'],$this->prevMonth['year']); | |
$this->prevMonth['month-name'] = static::monthName($this->prevMonth['year'].'-'.$this->prevMonth['month']); | |
} | |
/** | |
* This function is WordPress compatible | |
* It uses date_i18n function where available | |
*/ | |
private static function monthName($date) | |
{ | |
//WordPress compatible | |
if( function_exists('date_i18n') ) { | |
return date_i18n( 'F',strtotime($date) ); | |
} | |
return date( 'F',strtotime($date) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment