Location: PHPKode > scripts > DateRange > dateRange.php
<?php
/*******************************************************************

Class: dateRange
Author: B. Rock (hide@address.com)
Homepage: http://www.earn-web-cash.com
Date Created: Jan. 20, 2008
Version: 1.0

Description:  This class was created to hold a range of dates and
make it easy to check if a given date is within that range.  You can
also set a series of dates to be excluded from this range.

For example, create a range of "December 21, 2007" to "March 21, 2008"
and call it "Spring."  You could then check to see if a given date is
within the date range that you called "Spring."
*******************************************************************/

/*******************************************************************
Copyright Info:

Copyright 2008, Brian Rock.

    This php script is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*******************************************************************/

/*******************************************************************
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() {

	}
}

?>
Return current item: DateRange