class_MySqlHelper.inc.php 4.2 KB
<?php
/**
 * @file	MySqlHelper.inc.php
 * @category freeSN
 * @mailto	code [at] netz.coop
 * @version 0.4.200901
 * @link http://netz.coop
 * 
 *  @copyright Copyright by netz.coop e.G. 2015
 *  
 *
 *   This program 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 MySqlHelper {
	/**
	 * transform MySql DateTime format to Array
	 */
	public static function dateTimeToArray($param_datetime) {
		if($param_datetime) {
			$var_resultArray = array();

			$var_DateTimeArray = preg_split('/ /', $param_datetime, -1,  PREG_SPLIT_NO_EMPTY  );
			$var_resultArray['date'] = $var_DateTimeArray[0];
			$var_resultArray['time'] = $var_DateTimeArray[1];

			$var_DateArray = preg_split('/\W/', $var_resultArray['date'], -1,  PREG_SPLIT_NO_EMPTY  );
			$var_resultArray['year'] = $var_DateArray[0];
			$var_resultArray['month'] = $var_DateArray[1];
			$var_resultArray['day'] = $var_DateArray[2];

			$var_TimeArray = preg_split('/\W/', $var_resultArray['time'], -1,  PREG_SPLIT_NO_EMPTY  );
			$var_resultArray['hour'] = $var_TimeArray[0];
			$var_resultArray['minute'] = $var_TimeArray[1];
			$var_resultArray['second'] = $var_TimeArray[2];

			return $var_resultArray;
		} else {
			return false;
		}
	}

	/**
	 * transform Array to MySql DateTime format
	 */
	public static function arrayToDateTime(array $param_array) {
		if($param_array) {
			if(array_key_exists('date', $param_array) && array_key_exists('time', $param_array)) {
				$var_DateTime = $param_array['date'].' '.$param_array['time'];

			} else if(array_key_exists('year', $param_array) && array_key_exists('month', $param_array) && array_key_exists('day', $param_array)
			&& array_key_exists('hour', $param_array) && array_key_exists('minute', $param_array) && array_key_exists('second', $param_array)) {
				$var_DateTime = $param_array['year'].'-'.$param_array['month'].'-'.$param_array['day'].' '.$param_array['hour'].':'.$param_array['minute'].':'.$param_array['second'];

			} else {
				return false;
			}
			return $var_DateTime;
		} else {
			return false;
		}
	}

	public static function dateTimeToUnixTimestamp($param_datetime) {
		if(!is_array($param_datetime)) {
			$param_datetime = self::dateTimeToArray($param_datetime);
		}
		$var_unixtime = mktime($param_datetime['hour'], $param_datetime['minute'], $param_datetime['second'],$param_datetime['month'] , $param_datetime['day']  , $param_datetime['year']);
		return $var_unixtime;
	}

	public static function unixTimestampToDateTime($param_unixtime) {
		return date('Y-m-d H:i:s', $param_unixtime);
	}

	public static function addToDateTime($param_datetime,  $param_Add) {
		$var_unixtime = self::dateTimeToUnixTimestamp($param_datetime);

		if(is_array($param_Add)) {
			if(array_key_exists('hour', $param_Add)) {
				$var_unixtime = strtotime  ( $param_Add['hour'].' hour'  ,$var_unixtime );
			}
			if(array_key_exists('minute', $param_Add)) {
				$var_unixtime = strtotime  ( $param_Add['minute'].' minute'  ,$var_unixtime );
			}
			if(array_key_exists('second', $param_Add)) {
				$var_unixtime = strtotime  ( $param_Add['second'].' second'  ,$var_unixtime );
			}
			if(array_key_exists('month', $param_Add)) {
				$var_unixtime = strtotime  ( $param_Add['month'].' month'  ,$var_unixtime );
			}
			if(array_key_exists('day', $param_Add)) {
				$var_unixtime = strtotime  ( $param_Add['day'].' day'  ,$var_unixtime );
			}
			if(array_key_exists('year', $param_Add)) {
				$var_unixtime = strtotime  ( $param_Add['year'].' year'  ,$var_unixtime );
			}
			if(array_key_exists('week', $param_Add)) {
				$var_unixtime = strtotime  ( $param_Add['week'].' week'  ,$var_unixtime );
			}
		}
		return self::unixTimestampToDateTime($var_unixtime);
	}
}
?>