class_MySqlHelper.inc.php
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?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);
}
}
?>