<?php
/**
* @license mpl by ERB software
* @author stefan.erb(at)erb-software.com
* @example see below
*/
/**
* Class for a periode
*/
class Periode{
protected $start;
protected $end;
protected $perideName;
/**
* construct a entity of this class
* @param int $start
* @param int $end
* @param string $perideName
*/
public function __construct($start, $end, $perideName){
$this->start = $start;
$this->end = $end;
$this->perideName = $perideName;
}
/**
* return a timestamp from day, month[, year]
* @param int $day
* @param int $month
* @param int $year
* @return int
* @access public
*/
public static function createTime($day, $month, $year = 0){
return mktime(0, 0, 0, $month, $day, $year);
}
/**
* compare a timestamp with the periode
*
* @param int $termin
* @return boolean
* @access public
*/
public function compare($date = time){
//set the year for the start- and enddate
$start = $this->changeYear($this->start, idate("Y",$date));
$end = $this->changeYear($this->end, idate("Y",$date));
#$this->debugDates($termin, $start, $end, "step1");
//change the year if the startdate is in the future of the termin
if ($start > $date){
$this->addYear($start, -1);
$this->addYear($end, -1);
}
//change the endyear if the start is in the future of the end
if ($end < $start) {
$this->addYear($end, +1);
}
# $this->debugDates($termin, $start, $end, "step2");
return ($start <= $date && $date <= $end);
}
/**
* getter for perideName
* @return string
* @access public
*/
public function getPerideName(){
return $this->perideName;
}
/**
* get a timestamp from a other timestamp with a new year
* @param int $time
* @param int $year
* @return int
* @access protected
*/
protected function changeYear($time, $year){
return $this->createTime(idate("d", $time), iDate("m", $time), $year);
}
/**
* add years to a refereced timestamp
* @param int $time
* @param int $add
* @access protected
*/
protected function addYear(&$time, $add){
$time = $this->createTime(idate("d", $time), iDate("m", $time), idate("Y", $time) + $add);
}
/**
* Debug the periode and the time
* @param int $termin
* @param int $start
* @param int $end
* @param string $tag
* @access protected
*/
protected function debugDates($termin, $start, $end, $tag = ""){
echo $tag.": termin ".date("d.m.Y", $termin)." between ".date("d.m.Y", $start)." and ".date("d.m.Y", $end)."<br />";
}
}
# --------------- Example --------------------
/**
* a small testfunction
*/
function testDate($date, $periodes){
echo date("d.m.Y", $date).":<br />";
foreach($periodes as $i => $periode) {
echo $periode->compare($date) ? "{$periode->getPerideName()} <br />" : "";
}
}
/** create the testperiodes */
$periodes = array();
array_push($periodes, new Periode(Periode::createTime(25,12), Periode::createTime(02,01),"Weihnachten"));
array_push($periodes, new Periode(Periode::createTime(25,10), Periode::createTime(02,11),"Halloween"));
/** tes different dates */
testDate(Periode::createTime(26,12,2009), $periodes);
testDate(Periode::createTime(01,01,2010), $periodes);
testDate(Periode::createTime(26,10,2012), $periodes);
?>