Location: PHPKode > scripts > QValidation > qvalidation/Validation/DataType/Date.php
<?php

class Validation_DataType_Date extends Validation implements ValidInterface {

	public static function getInstance(){
		return new Validation_DataType_Date;
	}

	/**
	 * validate
	 * @desc Checks american value format correct syntax of value: yyyy-mm-dd
	 * @param	string	$value
	 * @return	boolean
	 */
	public static function validate($value) {
		if (preg_match('/[1-2][0-9]{3}-[0-1][0-9]-[0-3][0-9]/', $value)) {
			return true;
		}
		return false;
	}

	/**
	 * validateStrict
	 * @desc Checks american value format for correct syntax and existence
	 * correct syntax of value: yyyy-mm-dd
	 * @param	string	$value
	 * @return	boolean
	 */
	public static function validateStrict($value) {
		if (preg_match('/[1-2][0-9]{3}-[0-1][0-9]-[0-3][0-9]/', $value)) {
			$tmpDate = explode('-', $value);
			if (checkdate($tmpDate[1], $tmpDate[2], $tmpDate[0])) {
				return true;
			}
		}
		return false;
	}

	/**
	 * validateUnixtime
	 * validate an unix timestamp on range
	 * @desc "2038-01-19 04:14:07" = 2147483647 (max. valid unix timestamp)
	 * @param integer $value
	 * @return bool
	 */
	public static function validateUnixtime($value) {
		$check = Validation_DataType_Integer::validateInt($value);
		if($check and $value > -1 and $value < 2147483648) {
			return false;
		}
		return true;
	}

	/**
	 * validateDatetime
	 * @desc validate american and german datetime strings
	 * parses timestamp and checks it against unixtime
	 * @param string $value
	 * @return bool
	 */
	public static function validateDatetime($value) {
		$day = $month = $year = $hour = $minute = $second = '';
		if (strstr($value,'.')!==FALSE && count($arrExplode=preg_split('/[-| |\:]/',$value))==6) { //de
			$day 	= $arrExplode[0];
			$month 	= $arrExplode[1];
			$year 	= $arrExplode[2];
			$hour 	= $arrExplode[3];
			$minute = $arrExplode[4];
			$second = $arrExplode[5];
		} elseif (strstr($value,'-')!==FALSE && count($arrExplode=preg_split('/[\.| |\:]/',$value))==6) { // us
			
			$year 	= $arrExplode[0];
			$month 	= $arrExplode[1];
			$day 	= $arrExplode[2];
			$hour 	= $arrExplode[3];
			$minute = $arrExplode[4];
			$second = $arrExplode[5];			
		} else {
			return false;
		}
		
		$stamp = $year . "-" . $month . "-" . $day . " " .$hour .":". $minute . ":". $second;
		
		return self::validateUnixtime(strtotime($stamp));
	}

	/**
	 * validateDatetime
	 * @desc validate american and german datetime strings
	 * parses timestamp and checks it against unixtime
	 * @param string $value
	 * @return bool
	 */
	public static function validateDate($value) {
		$day = $month = $year = $hour = $minute = $second = '';
		if (strstr($value,'.')!==FALSE && count($arrExplode=preg_split('/[\.]/',$value))==3) { //de
			$day 	= $arrExplode[0];
			$month 	= $arrExplode[1];
			$year 	= $arrExplode[2];
		} elseif (strstr($value,'-')!==FALSE && count($arrExplode=preg_split('/[-]/',$value))==3) { // us

			$year 	= $arrExplode[0];
			$month 	= $arrExplode[1];
			$day 	= $arrExplode[2];
			$stamp = $year . "-" . $month . "-" . $day . " " .$hour;
		} else {
			return false;
		}
		return self::validateUnixtime(strtotime($stamp));
	}

	/**
	 * validateHour
	 * @desc validate an integer field in range from 0 to 24
	 * @param integer $value
	 * @return bool
	 */
	public static function validateHour($value) {
		$check = Validation_DataType_Integer::validateInt($value);
		if($check) {
			return Validation_DataType_Integer::validateOuterRange($value, 0, 23);
		}
		return $check;
	}

	/**
	 * validateTime
	 * @desc validate time string if it is in range
	 * @param string $value
	 * @return bool
	 */
	public static function validateTime($value) {
		$hour = $minute = '';
		$arrExplode=explode(':',$value);
		if (strstr($value,':')!==FALSE && count($arrExplode)<=3 && count($arrExplode)>1) {
			$hour = (int)substr($value,0,2);
			$minute = (int)substr($value,3,2);			
			if(
				Validation_DataType_Integer::validateOuterRange($hour, 0, 23) and
				Validation_DataType_Integer::validateOuterRange($minute, 0, 59)
				) {
					return true;
				}
		}
		return false;
	}

	/**
	 *
	 * @param string $value
	 * @return bool
	 */
	public static function isDatetime($value) {
		return (strtotime($value)>0?true:false);
		$timestamp = self::parseDatetime($value);
		return (false !== $timestamp);
	}

	private static function parseDatetime($value) {
		$res = strptime($value, '%Y-%m-%d %T');
		if (!$res) {
			return false;
		}
		return mktime($res['tm_hour'], $res['tm_min'], $res['tm_sec'], 1, $res['tm_yday']+1, $res['tm_year']+1900);
	}

	/**
	 * @param string $s
	 * @return bool
	 */
	public static function isDate($value) {
		$timestamp = -1;
		$d = 0;
		$m = 0;
		$y = 0;
		if (strlen($value) == 10) {
			if (strpos($value, "-") !== false) {
				$d = substr($value, 8, 2);
				$m = substr($value, 5, 2);
				$y = substr($value, 0, 4);
			} else
			if (strpos($s, ".") !== false) {
				$d = substr($value, 0, 2);
				$m = substr($value, 3, 2);
				$y = substr($value, 6, 4);
			} else {
				return false;
			}
		} else if (strlen($s) == 8) {
			$d = substr($value, 6, 2);
			$m = substr($value, 4, 2);
			$y = substr($value, 0, 4);
		} else {
			return false;
		}

		if (eregi("[^0-9]", $d) !== false || eregi("[^0-9]", $m) !== false || eregi("[^0-9]", $y) !== false) {
			return false;
		}

		$errold = error_reporting(0);
		$timestamp = mktime(0, 0, 0, $m, $d, $y);
		error_reporting($errold);
		if ($timestamp == -1) {
			return false;
		}

		return checkdate($m, $d, $y);
	}

}
Return current item: QValidation