* @author Lorenzo Alberton * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) * @version CVS: $Id: Weekday.php 300729 2010-06-24 12:05:53Z quipo $ * @link http://pear.php.net/package/Calendar */ /** * Allows Calendar include path to be redefined * @ignore */ if (!defined('CALENDAR_ROOT')) { define('CALENDAR_ROOT', 'Calendar'.DIRECTORY_SEPARATOR); } /** * Load Calendar decorator base class */ require_once CALENDAR_ROOT.'Decorator.php'; /** * Load a Calendar_Day */ require_once CALENDAR_ROOT.'Day.php'; /** * Decorator for fetching the day of the week * * $Day = new Calendar_Day(2003, 10, 23); * $Weekday = new Calendar_Decorator_Weekday($Day); * $Weekday->setFirstDay(0); // Set first day of week to Sunday (default Mon) * echo $Weekday->thisWeekDay(); // Displays 5 - fifth day of week relative to Sun * * * @category Date and Time * @package Calendar * @author Harry Fuecks * @author Lorenzo Alberton * @copyright 2003-2007 Harry Fuecks, Lorenzo Alberton * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause) * @link http://pear.php.net/package/Calendar * @access public */ class Calendar_Decorator_Weekday extends Calendar_Decorator { /** * First day of week * @var int (default = 1 for Monday) * @access private */ var $firstDay = 1; /** * Constructs Calendar_Decorator_Weekday * * @param object &$Calendar subclass of Calendar * * @access public */ function Calendar_Decorator_Weekday(&$Calendar) { parent::Calendar_Decorator($Calendar); } /** * Sets the first day of the week (0 = Sunday, 1 = Monday (default) etc) * * @param int $firstDay first day of week * * @return void * @access public */ function setFirstDay($firstDay) { $this->firstDay = (int)$firstDay; } /** * Returns the previous weekday * * @param string $format (default = 'int') return value format * * @return int $format numeric day of week or timestamp * @access public */ function prevWeekDay($format = 'int') { $ts = $this->calendar->prevDay('timestamp'); $Day = new Calendar_Day(2000, 1, 1); $Day->setTimeStamp($ts); $day = $this->calendar->cE->getDayOfWeek( $Day->thisYear(), $Day->thisMonth(), $Day->thisDay() ); $day = $this->adjustWeekScale($day); return $this->returnValue('Day', $format, $ts, $day); } /** * Returns the current weekday * * @param string $format (default = 'int') return value format * * @return int numeric day of week or timestamp * @access public */ function thisWeekDay($format = 'int') { $ts = $this->calendar->thisDay('timestamp'); $day = $this->calendar->cE->getDayOfWeek( $this->calendar->year, $this->calendar->month, $this->calendar->day ); $day = $this->adjustWeekScale($day); return $this->returnValue('Day', $format, $ts, $day); } /** * Returns the next weekday * * @param string $format (default = 'int') return value format * * @return int numeric day of week or timestamp * @access public */ function nextWeekDay($format = 'int') { $ts = $this->calendar->nextDay('timestamp'); $Day = new Calendar_Day(2000, 1, 1); $Day->setTimeStamp($ts); $day = $this->calendar->cE->getDayOfWeek( $Day->thisYear(), $Day->thisMonth(), $Day->thisDay() ); $day = $this->adjustWeekScale($day); return $this->returnValue('Day', $format, $ts, $day); } /** * Adjusts the day of the week relative to the first day of the week * * @param int $dayOfWeek day of week calendar from Calendar_Engine * * @return int day of week adjusted to first day * @access private */ function adjustWeekScale($dayOfWeek) { $dayOfWeek = $dayOfWeek - $this->firstDay; if ($dayOfWeek >= 0) { return $dayOfWeek; } else { return $this->calendar->cE->getDaysInWeek( $this->calendar->year, $this->calendar->month, $this->calendar->day ) + $dayOfWeek; } } } ?>