-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOgerDateTime.class.php
More file actions
190 lines (142 loc) · 3.82 KB
/
OgerDateTime.class.php
File metadata and controls
190 lines (142 loc) · 3.82 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?PHP
/*
#LICENSE BEGIN
**********************************************************************
* OgerArch - Archaeological Database is released under the GNU General Public License (GPL) <http://www.gnu.org/licenses>
* Copyright (C) Gerhard Öttl <gerhard.oettl@ogersoft.at>
**********************************************************************
#LICENSE END
*/
/**
* Date time extension
*/
class OgerDateTime extends DateTime {
public static $defaultDateFormat = "d.m.Y";
public static $defaultTimeFormat = "H:i";
public static $defaultDateTimeFormat = "d.m.Y H:i";
/**
* Constructor.
* Additional formats:
* - numeric-only $time parameter is handled as unix timestamp.
*/
public function __construct ($time = "now", $timezone = null) {
if (is_numeric($time)) {
$time = "@{$time}";
}
parent::__construct($time, $timezone);
} // eo constructor
/**
* Format date part with default setting.
*/
public function formatDate($format = "") {
$format = ($format ?: static::$defaultDateFormat);
return parent::format($format);
} // eo format date
/**
* Format time part with default setting.
*/
public function formatTime($format = "") {
$format = ($format ?: static::$defaultTimeFormat);
return parent::format($format);
} // eo format time
/**
* Format date and time with default setting.
*/
public function format($format = "") {
$format = ($format ?: static::$defaultDateTimeFormat);
return parent::format($format);
} // eo format date and time
/**
* Format date part into ansi format.
*/
public function formatAnsiDate() {
return parent::format("Y-m-d");
} // eo format ansi date
/**
* Check if time is empty
*/
public static function _isEmpty($timeStr, $opts = array()) {
$timeStr = trim($timeStr);
if (!$timeStr) {
return true;
}
// check sql date (maybe can be improved ???)
if (substr($timeStr, 0, 10) == "0000-00-00" && !$opts['allowZeroYear']) {
return true;
}
// invalid date is reported empty
// numeric values are handled as already parsed with strtotime
if (!is_numeric($timeStr) && strtotime($timeStr) === false) {
return true;
}
return false;
} // eo is empty
/**
* Date difference in full days (no fractals).
*/
public function diffDays($dateTime, $absolute = false) {
$interval = $this->diff($dateTime, $absolute);
$days = $interval->days;
if (!$absolute && $interval->invert) {
$days *= -1;
}
return $days;
} // eo day diff
/*
* Get diff with extended parameter handling
* Date1 and date2 can be string or DateTime objects.
* Date1 before date2 - otherwise the interval is negative.
* Empty dates are replaced by NOW.
* Invalid dates result in null.
* Return DateInterval object.
*/
public static function _xDiff($date1 = null, $date2 = null) {
if (static::_isEmpty($date1)) {
$date1 = "NOW";
}
if (is_string($date1)) {
$date1 = new DateTime($date1);
if (!$date1) { // invalid date
return null;
}
}
if (static::_isEmpty($date2)) {
$date2 = "NOW";
}
if (is_string($date2)) {
$date2 = new DateTime($date2);
if (!$date2) { // invalid date
return null;
}
}
return $date1->diff($date2);
} // eo get x-diff
/*
* Static function for formated date output
* Date can be string, a unix timestamp or DateTime object.
*/
public static function _formatDate($dateIn, $format = null) {
if (!$dateIn) {
return "";
}
$format = ($format ?: static::$defaultDateFormat);
if (is_string($dateIn)) {
if (OgerDateTime::_isEmpty($dateIn)) {
return "";
}
$date = new DateTime($dateIn);
if (!$date) { // invalid date
return "";
}
}
elseif (is_numeric($dateIn)) {
$date = new DateTime();
$date.setTimestamp($dateIn);
}
else {
$date = $dateIn;
}
return $date->format($format);
} // eo static date format
} // eo class
?>