class_mcDate.inc.php 2.41 KB
<?php
class mcDate {
	/**
	 * check if the format is like this yyyy-mm-dd
	 */
	public static function check8601Date($param_date) {
		$datumfalsch = 0;
		$pruefjahr = substr($param_date, 0, 4);
		$pruefstrich1 = substr($param_date, 4, 1);
		$pruefmonat = substr($param_date, 5, 2);
		$pruefstrich2 = substr($param_date, 7, 1);
		$prueftag = substr($param_date, 8, 2);
		
		if (!eregi("^[0-9]+$", $pruefjahr))
			$datumfalsch = 1;
		if ($pruefstrich1 != "-")
			$datumfalsch = 1;;
		if ($pruefstrich2 != "-")
			$datumfalsch = 1;;
		if ($pruefmonat < 01 || $pruefmonat > 12)
			$datumfalsch = 1;
		if ($prueftag < 01)
			$datumfalsch = 1;

		if ($datumfalsch == 0 && !checkdate($pruefmonat, $prueftag, $pruefjahr)) {
			$datumfalsch = 1;
		}

		if($datumfalsch==1) {
			 return false;
		} else {
			return true;
		}
	}
	/**
	 * wants http://en.wikipedia.org/wiki/ISO_8601 format for a date
	 */
	public static function format8601Date($param_date) {
		if($param_date == '0000-00-00') {
			return '';
		} else {
			$pruefjahr = substr($param_date, 0, 4);
			$pruefmonat = substr($param_date, 5, 2);
			$prueftag = substr($param_date, 8, 2);
			return $prueftag.'.'.$pruefmonat.'.'.$pruefjahr;
		}
	}

	public static function checkIfFirstIsBefore($param_DateBefore, $param_DateAfter) {
		if(mcDate::check8601Date($param_DateBefore) && mcDate::check8601Date($param_DateAfter)) {

			$DateBefore = mktime(null, null, null, substr($param_DateBefore, 5, 2), substr($param_DateBefore, 8, 2), substr($param_DateBefore, 0, 4));
			$DateAfter = mktime(null, null, null, substr($param_DateAfter, 5, 2), substr($param_DateAfter, 8, 2), substr($param_DateAfter, 0, 4));

			if($DateBefore <= $DateAfter) {
				return true;
			} else {
				return false;
			}

		} else {
			return false;
		}

	}
	
	
	public static function getNowAsDatetime() {
		$return = date('o-m-d H:i:s');
		return $return;
	}
	
	/**
	 *
	 * @param array $param_date 
	 * @return type 
	 */
	private static function transformToDatetime(array $param_date) {
		return ''.$param_date['Y'].'-'.$param_date['m'].'-'.$param_date['d'].' '.$param_date['H'].':'.$param_date['i'].':'.$param_date['s'].'';		
	}

	public static function transformToArrayDatetime($param_date=null) {
		if($param_date) {
			if(is_array($param_date)) {
				$tmp_date = mcDate::transformToDatetime($param_date);
			} else {
				$tmp_date = $param_date;
			}

		} else {
			$tmp_date = mcDate::getNowAsDatetime();
		}
		return $tmp_date;
	}	
	
}
?>