Time filtering: PHP version

As mentioned before, I originally wrote my time filter in PHP but converted it to C# for speed. So here it is for posterity and reference.

class nonWorkRanges {
	public $parse;
	public $start;
	public $end;

	/*
		this::start and this::end stucture

		day = 1...31
		month = 1...12 (xx = reoccurring)
		year = YYYY (xxxx = reoccurring)
		weekday = 0...6 (sunday...saturday, if this is set we ignore the day, month, and year values)
		hour = 0...23
		minute = 0...59
		second = 0...59
	*/

	public function businessTime($created, $response) {
		$start = $this->start;
		$end = $this->end;

		if(isset($this->start['weekday']) and isset($this->end['weekday'])) {
			$ticks = 0;
			$days = date('z', $response) - date('z', $created) + 1;

			$basetime = mktime(0, 0, 0, date('m',$created), date('j',$start['weekday']), date('Y',$created));

			for($dayi=0; $dayi<$days;$dayi++) {

				$day_start = mktime(0, 0, 0, date('m',$basetime), date('j', $basetime)+$dayi, date('Y',$basetime));
				$day_end = mktime(23, 59, 59, date('m',$basetime), date('j', $basetime)+$dayi, date('Y',$basetime));
				$dow = date('w', $day_start);

				if($start['weekday'] < $end['weekday'] && ($dow < $start['weekday'] || $dow > $end['weekday'])) {
					continue;
				}

				if($start['weekday'] > $end['weekday'] && ($dow > $start['weekday'] || $dow < $end['weekday'])) {
					//var_dump($start['weekday'] < $end['weekday'], ($dow > $start['weekday'] || $dow < $end['weekday']));
					continue;
				}

				$mask_start = mktime($start['hour'], $start['minute'], $start['second'], date('m',$day_start), ((date('j',$day_start)-date('w',$day_start))+$start['weekday']), date('Y',$day_start));
				$mask_end = mktime($end['hour'], $end['minute'], $end['second'], date('m',$day_start), ((date('j',$day_start)-date('w',$day_start))+$end['weekday']), date('Y',$day_start));

				for($i=0; $i<86400; $i++) {
					$loc = $day_start + $i;
					$tf = array(
						$loc < $created,
						$loc > $response,
						$loc < $mask_start,
					 	$loc > $mask_end
					);
					if((!$tf[0] && !$tf[1]) && (!$tf[2] && !$tf[3])) {
						$ticks++;
					}
				}
			}
			return $ticks;
		}
	}
}
You can leave a response, or trackback from your own site.

Leave a Reply