| date Range- -Class for PHP | | Print | |
/*******************************************************************
Class Declarations:
Properties:
private $start;
private $end;
private $name;
private $exclusions = array();
Methods:
public function __construct($startDate, $endDate, $rangeName = 'Random Date Range', $excludedDates = array());
public function getName();
public function getStart();
public function getEnd();
public function addExclusion ($exclude);
private function isExcluded ($date);
public function inRange($date);
public function __destruct();
*******************************************************************/
class dateRange {
private $start;
private $end;
private $name;
private $exclusions = array();
// $startDate and $endDate should be timestamps or formatted strings
// The constructor will attempt to convert any strings to timestamps
// $rangeName should be the desired name for the dateRange
// $excludedDates should be an array of dates (timestamps) to be excluded
// We'll attempt to convert any strings in this to timestamps if possible
public function __construct($startDate, $endDate, $rangeName = 'Random Date Range', $excludedDates = array()) {
if (is_string($startDate)) {
$this->start = strtotime($startDate);
} else {
$this->start = $startDate;
}
if (is_string($endDate)) {
$this->end = strtotime($endDate);
} else {
$this->end = $endDate;
}
$this->name = $rangeName;
$this->addExclusion($excludedDates);
}
public function getName() {
return $this->name;
}
public function getStart() {
return $this->start;
}
public function getEnd() {
return $this->end;
}
// $exclude can be a single timestamp to add to exclusions,
// or an array of timestamps
public function addExclusion ($exclude) {
if (is_array($exclude)) {
foreach ($exclude as $newExclude) {
if (is_string($newExclude)) {
$this->exclusions[] = strtotime($newExclude);
} else {
$this->exclusions[] = $newExclude;
}
}
} else {
if (is_string($exclude)) {
$this->exclusions[] = strtotime($exclude);
} else {
$this->exclusions[] = $exclude;
}
}
}
// If $date is in the $exclusions array, return true. Otherwise false.
private function isExcluded ($date) {
foreach ($this->exclusions as $exclude) {
if ($date == $exclude)
return true;
}
return false;
}
// $date could be a timestamp or string. It's automatically converted
// to a timestamp. Check to see if it's in the range and not excluded.
// If it is, return true. Otherwise, return false.
public function inRange($date) {
if (is_string($date)) {
$date = strtotime($date);
}
if (($this->start <= $date) && ($date <= $this->end)) {
// Make a separate check, in case you want to return something
// to say that it's in range but excluded
if (!$this->isExcluded($date)) {
return true;
}
} else {
return false;
}
}
// We'll let php automatically clean stuff up, but I felt like declaring this
public function __destruct() {
}
}
?>
| Users' Comments (0) |
|
No comment posted






