<?php
class CalendarIterator implements Iterator {
private $_ = array();
public function __construct($array=array()) {
$this->_ = $array;
}
function __toString() {
$result = '';
foreach($this->_ as $date) {
$result .= $date . '<br />';
}
return $result;
}
function rewind() {
reset($this->_);
}
function current() {
return current($this->_);
}
function key() {
return key($this->_);
}
function next() {
return next($this->_);
}
function prev() {
return prev($this->_);
}
function valid() {
$key = key($this->_);
$var = ($key !== null && $key !== false);
return $var;
}
function count() {
return count($this->_);
}
function first() {
return array_shift($this->_);
}
function last() {
return array_pop($this->_);
}
function nth($n) {
$values = array_values($this->_);
return isset($values[$n]) ? $values[$n] : null;
}
function indexOf($needle) {
return array_search($needle, array_values($this->_));
}
function toArray() {
return $this->_;
}
function slice($offset=null, $limit=null) {
if($offset === null && $limit === null) return $this;
return new CalendarIterator(array_slice($this->_, $offset, $limit));
}
function limit($limit) {
return $this->slice(0, $limit);
}
}
class CalendarObj {
var $yearINT;
var $monthINT;
var $dayINT;
var $hourINT;
var $minuteINT;
var $secondINT;
var $timestamp = 0;
function __construct($year=false, $month=1, $day=1, $hour=0, $minute=0, $second=0) {
if(!$year) $year = date('Y');
if(!$month) $month = date('m');
if(!$day) $day = date('d');
$this->yearINT = intval($year);
$this->monthINT = intval($month);
$this->dayINT = intval($day);
$this->hourINT = intval($hour);
$this->minuteINT = intval($minute);
$this->secondINT = intval($second);
// convert this to timestamp
$this->timestamp = mktime($hour, $minute, $second, $month, $day, $year);
}
function year($year=false) {
if(!$year) $year = $this->yearINT;
return new CalendarYear($year, 1, 1, 0, 0, 0);
}
function month($month=false) {
if(!$month) $month = $this->monthINT;
return new CalendarMonth($this->yearINT, $month, 1, 0, 0, 0);
}
function day($day=false) {
if(!$day) $day = $this->dayINT;
return new CalendarDay($this->yearINT, $this->monthINT, $day, 0, 0, 0);
}
function hour($hour=false) {
if(!$hour) $hour = $this->hourINT;
return new CalendarHour($this->yearINT, $this->monthINT, $this->dayINT, $hour, 0, 0);
}
function minute($minute=false) {
if(!$minute) $minute = $this->minuteINT;
return new CalendarMinute($this->yearINT, $this->monthINT, $this->dayINT, $this->hourINT, $minute, 0);
}
function second($second=false) {
if(!$second) $second = $this->secondINT;
return new CalendarSecond($this->yearINT, $this->monthINT, $this->dayINT, $this->hourINT, $this->minuteINT, $second);
}
function timestamp() {
return $this->timestamp;
}
function __toString() {
return date('Y-m-d H:i:s', $this->timestamp);
}
function format($format) {
return date($format, $this->timestamp);
}
function iso() {
return date(DATE_ISO, $this->timestamp);
}
function cookie() {
return date(DATE_COOKIE, $this->timestamp);
}
function rss() {
return date(DATE_RSS, $this->timestamp);
}
function atom() {
return date(DATE_ATOM, $this->timestamp);
}
function mysql() {
return date('Y-m-d H:i:s', $this->timestamp);
}
function time() {
return strftime('%T', $this->timestamp);
}
function ampm() {
return strftime('%p', $this->timestamp);
}
function modify($string) {
$ts = (is_int($string)) ? $this->timestamp+$string : strtotime($string, $this->timestamp);
list($year, $month, $day, $hour, $minute, $second) = explode('-', date('Y-m-d-H-i-s', $ts));
return new CalendarDay($year, $month, $day, $hour, $minute, $second);
}
function plus($string) {
$modifier = (is_int($string)) ? $string : '+' . $string;
return $this->modify($modifier);
}
function add($string) {
return $this->plus($string);
}
function minus($string) {
$modifier = (is_int($string)) ? -$string : '-' . $string;
return $this->modify($modifier);
}
function sub($string) {
return $this->minus($string);
}
function dmy() {
return $this->format('d.m.Y');
}
function padded() {
return str_pad($this->int(),2,'0',STR_PAD_LEFT);
}
}
class Calendar {
static $now = 0;
function __construct() {
Calendar::$now = time();
}
function years($start, $end) {
$array = array();
foreach(range($start, $end) as $year) {
$array[] = $this->year($year);
}
return new CalendarIterator($array);
}
function year($year) {
return new CalendarYear($year, 1, 1, 0, 0, 0);
}
function months($year=false) {
$year = new CalendarYear($year, 1, 1, 0, 0, 0);
return $year->months();
}
function month($year, $month) {
return new CalendarMonth($year, $month, 1, 0, 0);
}
function week($year=false, $week=false) {
return new CalendarWeek($year, $week);
}
function days($year=false) {
$year = new CalendarYear($year);
return $year->days();
}
function day($year=false, $month=false, $day=false) {
return new CalendarDay($year, $month, $day);
}
function date() {
$args = func_get_args();
if(count($args) > 1) {
$year = isset($args[0]) ? $args[0] : false;
$month = isset($args[1]) ? $args[1] : 1;
$day = isset($args[2]) ? $args[2] : 1;
$hour = isset($args[3]) ? $args[3] : 0;
$minute = isset($args[4]) ? $args[4] : 0;
$second = isset($args[5]) ? $args[5] : 0;
} else {
if(isset($args[0])) {
$ts = (is_int($args[0])) ? $args[0] : strtotime($args[0]);
} else {
$ts = time();
}
if(!$ts) return false;
list($year, $month, $day, $hour, $minute, $second) = explode('-', date('Y-m-d-H-i-s', $ts));
}
return new CalendarDay($year, $month, $day, $hour, $minute, $second);
}
function today() {
return $this->date('today');
}
function now() {
return $this->today();
}
function tomorrow() {
return $this->date('tomorrow');
}
function yesterday() {
return $this->date('yesterday');
}
}
class CalendarYear extends CalendarObj {
function __toString() {
return $this->format('Y');
}
function int() {
return $this->yearINT;
}
function months() {
$array = array();
foreach(range(1, 12) as $month) {
$array[] = $this->month($month);
}
return new CalendarIterator($array);
}
function month($month=1) {
return new CalendarMonth($this->yearINT, $month);
}
function weeks() {
$array = array();
$weeks = (int)date('W', mktime(0,0,0,12,31,$this->int))+1;
foreach(range(1,$weeks) as $week) {
$array[] = new CalendarWeek($this, $week);
}
return new CalendarIterator($array);
}
function week($week=1) {
return new CalendarWeek($this, $week);
}
function countDays() {
return (int)date('z', mktime(0,0,0,12,31,$this->yearINT))+1;
}
function days() {
$days = $this->countDays();
$array = array();
$ts = false;
for($x=0; $x<$days; $x++) {
$ts = (!$ts) ? $this->timestamp : strtotime('tomorrow', $ts);
$month = date('m', $ts);
$day = date('d', $ts);
$array[] = $this->month($month)->day($day);
}
return new CalendarIterator($array);
}
function next() {
return $this->plus('1year')->year();
}
function prev() {
return $this->minus('1year')->year();
}
function name() {
return
评论1
最新资源