<?php
/**
* CalendarFrame
*
* Generate a simple to work with calendar structure.
*
* All the calendar classes I see around the internet are designed for one
* specific task. I wanted a system that I could use for multiple projects
* without having to hack the code from another script. CalendarFrame allows
* you to generate a calendar by Year, Month, Week, and even get a group of
* Days. The returned structure is an array, so you are free to display the
* data in any form you wish. I use this system to generate XML and JSON files
* for use with AJAX. I also format this with HTML to make a simple calendar
* on sites I work on that require a displayed calendar system.
*
* @package Calendar
* @author Nathan Lucas <nathan@gimpstraw.com>
* @link http://www.gimpstraw.com/
* @copyright Copyright (c) 2008, Nathan Lucas
* @version 2.0.4
*/
class CalendarFrame {
/**
* Offset for which day your week starts. Sun-Sat.
*
* @access private
* @var integer
*/
private $startDay = 0;
/**
* CalendarFrame($startDay)
*
* Sets the start day offset. If the start day is not
* specified, it defaults to Sunday.
*
* @param string $startDay
* @access public
* @return void
*/
public function __construct($startDay = "sunday") {
$day = array();
$day['sun'] = 0;
$day['mon'] = 1;
$day['tue'] = 2;
$day['wed'] = 3;
$day['thu'] = 4;
$day['fri'] = 5;
$day['sat'] = 6;
$this->startDay = $day[strtolower(substr($startDay, 0, 3))];
}
/**
* getYear($stamp)
*
* Generates an array of all months, Jan - Dec, of a given year.
* If $stamp is not specified, $stamp defaults to the current
* year.
*
* @param mixed $stamp
* @access public
* @return array
*/
public function getYear($stamp = null) {
$stamp = $this->_parseStamp($stamp);
$stamp = mktime(0, 0, 0, 1, 1, date("Y", $stamp));
$months = array();
for ($i = 1; $i <= 12; $i++) {
$months[] = $this->getMonth(mktime(0, 0, 0, $i, 1, date("Y", $stamp)));
}
/**
* Array (
*
* previous_year_stamp
* current_stamp
* next_year_stamp
*
* month Array (
*
* previous_month_stamp
* current_stamp
* next_month_stamp
*
* week Array (
*
* previous_week_stamp
* current_stamp
* next_week_stamp
*
* day Array (
*
* day_stamp
* ...
*
* ...
*
* ...
*/
$out = array();
$out['previous'] = strtotime("-1 year", $stamp);
$out['stamp'] = $stamp;
$out['next'] = strtotime("+1 year", $stamp);
$out['month'] = $months;
return $out;
}
/**
* getMonth($stamp)
*
* Generates an array of a given month. If $stamp is not specified,
* $stamp defaults to the current month and year.
*
* @param mixed $stamp
* @access public
* @return array
*/
public function getMonth($stamp = null) {
$stamp = $this->_parseStamp($stamp);
$stamp = mktime(0, 0, 0, date("n", $stamp), 1, date("Y", $stamp));
$last_day = mktime(0, 0, 0, date("n", $stamp), date("t", $stamp), date("Y", $stamp));
$weeks = array();
$tmp_stamp = $stamp;
$tmp_last_day = $stamp;
$i = 0;
while ($tmp_last_day <= $last_day) {
$weeks[$i] = $this->getWeek($tmp_stamp);
$tmp_stamp = strtotime("+1 week", $tmp_stamp);
$tmp_last_day = $weeks[$i]['next'];
$i++;
}
/**
* Array (
*
* previous_month_stamp
* current_stamp
* next_month_stamp
*
* week Array (
*
* previous_week_stamp
* current_stamp
* next_week_stamp
*
* day Array (
*
* day_stamp
* ...
*
* ...
*/
$out = array();
$out['previous'] = strtotime("-1 month", $stamp);
$out['stamp'] = $stamp;
$out['next'] = strtotime("+1 month", $stamp);
$out['week'] = $weeks;
return $out;
}
/**
* getWeek($stamp)
*
* Generates an array of a given week. If $stamp is not specified,
* $stamp defaults to the current year and week.
*
* @param mixed $stamp
* @access public
* @return array
*/
public function getWeek($stamp = null) {
$stamp = $this->_parseStamp($stamp);
$day = date("j", $stamp);
$dow = (date("w", $stamp) - $this->startDay);
if ($dow < 1) {
$stamp = strtotime("-1 week -".($day - ($day - $dow))." days", $stamp);
} else {
$stamp = strtotime("-".($day - ($day - $dow))." days", $stamp);
}
$stop = strtotime("+6 days", $stamp);
/**
* Array (
*
* previous_week_stamp
* current_stamp
* next_week_stamp
*
* day Array (
*
* day_stamp
* ...
*/
$out = array();
$out['previous'] = strtotime("-1 week", $stamp);
$out['stamp'] = $stamp;
$out['next'] = strtotime("+1 week", $stamp);
$out['day'] = $this->getDays($stamp, $stop);
return $out;
}
/**
* getDays($start, $stop, $get)
*
* Generates an array of dates from $start to $stop. $get allows
* you to specify a certain day of the week:
*
* @example
*
* $calendar = new CalendarFrame();
* $sundays = $calendar->getDays(mktime(), "2008-12-25", "sundays");
* echo "There are ".count($sundays)." sundays until Christmas.";
*
* @param mixed $start
* @param mixed $stop
* @param string $get
* @access public
* @return array
*/
public function getDays($start, $stop, $get = null) {
$start = $this->_parseStamp($start);
$stop = $this->_parseStamp($stop);
$get = (is_null($get)) ? "+1 day" : "next ".substr(ucfirst($get), 0, 3);
$days = array();
$tmp_stamp = ($get != "+1 day") ? strtotime($get) : $start;
while ($tmp_stamp <= $stop) {
$days[] = $tmp_stamp;
$tmp_stamp = strtotime($get, $tmp_stamp);
}
/**
* Array (
*
* day_stamp
* ...
*/
return $days;
}
/**
* _parseStamp($stamp)
*
* Parses a given stamp into a UNIX timestamp. This enables you to
* use date formats supported under strtotime().
*
* @param mixed $stamp
* @access private
* @return unknown
*/
private function _parseStamp($stamp) {
if (is_null($stamp)) {
$stamp = mktime();
} else {
$stamp = (is_int($stamp)) ? $stamp : strtotime($stamp);
}
return $stamp;
}
}
?>
|