59177427 |
1 | <?php |
7350889b |
2 | |
35586184 |
3 | /** |
4 | * date.php |
5 | * |
35586184 |
6 | * Takes a date and parses it into a usable format. The form that a |
7 | * date SHOULD arrive in is: |
8 | * <Tue,> 29 Jun 1999 09:52:11 -0500 (EDT) |
9 | * (as specified in RFC 822) -- 'Tue' is optional |
10 | * |
c4faef33 |
11 | * @copyright 1999-2020 The SquirrelMail Project Team |
4b4abf93 |
12 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
31841a9e |
13 | * @version $Id$ |
d6c32258 |
14 | * @package squirrelmail |
371e2a0d |
15 | * @subpackage date |
35586184 |
16 | */ |
17 | |
202bcbcc |
18 | /** |
19 | * dependency information |
20 | * - none |
21 | */ |
35586184 |
22 | |
8b096f0a |
23 | /** |
24 | * Corrects a time stamp to be the local time. |
25 | * |
26 | * @param int stamp the timestamp to adjust |
27 | * @param string tzc the timezone correction |
28 | * @return int the corrected timestamp |
29 | */ |
d5c4ae7c |
30 | function getGMTSeconds($stamp, $tzc) { |
dc06f88c |
31 | /* date couldn't be parsed */ |
913ed9a3 |
32 | if ($stamp == -1) { |
33 | return -1; |
34 | } |
d5c4ae7c |
35 | /* timezone correction, expressed as `shhmm' */ |
36 | switch($tzc) |
dc06f88c |
37 | { |
38 | case 'Pacific': |
39 | case 'PST': |
d5c4ae7c |
40 | $tzc = '-0800'; |
dc06f88c |
41 | break; |
c2e29558 |
42 | case 'Mountain': |
43 | case 'MST': |
44 | case 'PDT': |
45 | $tzc = '-0700'; |
dc06f88c |
46 | break; |
47 | case 'Central': |
48 | case 'CST': |
c2e29558 |
49 | case 'MDT': |
50 | $tzc = '-0600'; |
dc06f88c |
51 | break; |
52 | case 'Eastern': |
53 | case 'EST': |
c2e29558 |
54 | case 'CDT': |
55 | $tzc = '-0500'; |
dc06f88c |
56 | break; |
57 | case 'EDT': |
d5c4ae7c |
58 | $tzc = '-0400'; |
dc06f88c |
59 | break; |
c2e29558 |
60 | case 'GMT': |
32ec5f13 |
61 | case 'UTC': |
c2e29558 |
62 | $tzc = '+0000'; |
dc06f88c |
63 | break; |
64 | case 'BST': |
d5c4ae7c |
65 | case 'MET': |
66 | case 'CET': |
67 | $tzc = '+0100'; |
68 | break; |
dc06f88c |
69 | case 'EET': |
c2e29558 |
70 | case 'IST': |
71 | case 'MET DST': |
72 | case 'METDST': |
c05566f6 |
73 | case 'MEST': |
74 | case 'CEST': |
c2e29558 |
75 | $tzc = '+0200'; |
dc06f88c |
76 | break; |
c2e29558 |
77 | case 'HKT': |
78 | $tzc = '+0800'; |
dc06f88c |
79 | break; |
c2e29558 |
80 | case 'JST': |
dc06f88c |
81 | case 'KST': |
c2e29558 |
82 | $tzc = '+0900'; |
dc06f88c |
83 | break; |
0e4ae91a |
84 | } |
d5c4ae7c |
85 | $neg = false; |
86 | if (substr($tzc, 0, 1) == '-') { |
0e4ae91a |
87 | $neg = true; |
d5c4ae7c |
88 | } else if (substr($tzc, 0, 1) != '+') { |
89 | $tzc = '+'.$tzc; |
0e4ae91a |
90 | } |
d5c4ae7c |
91 | $hh = substr($tzc,1,2); |
92 | $mm = substr($tzc,3,2); |
93 | $iTzc = ($hh * 60 + $mm) * 60; |
c2e29558 |
94 | if ($neg) $iTzc = -1 * (int) $iTzc; |
d5c4ae7c |
95 | /* stamp in gmt */ |
96 | $stamp -= $iTzc; |
4f51df66 |
97 | /* now find what the server is at */ |
0e4ae91a |
98 | $current = date('Z', time()); |
d5c4ae7c |
99 | /* stamp in local timezone */ |
100 | $stamp += $current; |
c2e29558 |
101 | |
0e4ae91a |
102 | return $stamp; |
103 | } |
0f1835f3 |
104 | |
0e4ae91a |
105 | /** |
8b096f0a |
106 | * Returns the (localized) string for a given day number. |
107 | * Switch system has been intentionaly chosen for the |
108 | * internationalization of month and day names. The reason |
109 | * is to make sure that _("") strings will go into the |
110 | * main po. |
111 | * |
112 | * @param int day_number the day number |
113 | * @return string the day in human readable form |
114 | */ |
0e4ae91a |
115 | function getDayName( $day_number ) { |
0f1835f3 |
116 | |
0e4ae91a |
117 | switch( $day_number ) { |
118 | case 0: |
119 | $ret = _("Sunday"); |
120 | break; |
121 | case 1: |
122 | $ret = _("Monday"); |
123 | break; |
124 | case 2: |
125 | $ret = _("Tuesday"); |
126 | break; |
127 | case 3: |
128 | $ret = _("Wednesday"); |
129 | break; |
130 | case 4: |
131 | $ret = _("Thursday"); |
132 | break; |
133 | case 5: |
134 | $ret = _("Friday"); |
135 | break; |
136 | case 6: |
137 | $ret = _("Saturday"); |
138 | break; |
139 | default: |
140 | $ret = ''; |
141 | } |
142 | return( $ret ); |
143 | } |
d59837cf |
144 | |
8b096f0a |
145 | /** |
146 | * Like getDayName, but returns the short form |
147 | * @param int day_number the day number |
148 | * @return string the day in short human readable form |
149 | */ |
22d73941 |
150 | function getDayAbrv( $day_number ) { |
151 | |
152 | switch( $day_number ) { |
153 | case 0: |
154 | $ret = _("Sun"); |
155 | break; |
156 | case 1: |
157 | $ret = _("Mon"); |
158 | break; |
159 | case 2: |
160 | $ret = _("Tue"); |
161 | break; |
162 | case 3: |
163 | $ret = _("Wed"); |
164 | break; |
165 | case 4: |
166 | $ret = _("Thu"); |
167 | break; |
168 | case 5: |
169 | $ret = _("Fri"); |
170 | break; |
171 | case 6: |
172 | $ret = _("Sat"); |
173 | break; |
174 | default: |
175 | $ret = ''; |
176 | } |
177 | return( $ret ); |
178 | } |
179 | |
8b096f0a |
180 | |
181 | /** |
182 | * Returns the (localized) string for a given month number. |
183 | * |
c2e29558 |
184 | * @param string month_number the month number (01..12) |
8b096f0a |
185 | * @return string the month name in human readable form |
186 | */ |
0e4ae91a |
187 | function getMonthName( $month_number ) { |
188 | switch( $month_number ) { |
189 | case '01': |
190 | $ret = _("January"); |
191 | break; |
192 | case '02': |
193 | $ret = _("February"); |
194 | break; |
195 | case '03': |
196 | $ret = _("March"); |
197 | break; |
198 | case '04': |
199 | $ret = _("April"); |
200 | break; |
201 | case '05': |
202 | $ret = _("May"); |
203 | break; |
204 | case '06': |
205 | $ret = _("June"); |
206 | break; |
207 | case '07': |
208 | $ret = _("July"); |
209 | break; |
210 | case '08': |
211 | $ret = _("August"); |
212 | break; |
213 | case '09': |
214 | $ret = _("September"); |
215 | break; |
216 | case '10': |
217 | $ret = _("October"); |
218 | break; |
219 | case '11': |
220 | $ret = _("November"); |
221 | break; |
222 | case '12': |
223 | $ret = _("December"); |
224 | break; |
225 | default: |
226 | $ret = ''; |
227 | } |
228 | return( $ret ); |
229 | } |
d59837cf |
230 | |
8b096f0a |
231 | /** |
232 | * Returns the (localized) string for a given month number, |
233 | * short representation. |
234 | * |
c2e29558 |
235 | * @param string month_number the month number (01..12) |
8b096f0a |
236 | * @return string the shortened month in human readable form |
237 | */ |
22d73941 |
238 | function getMonthAbrv( $month_number ) { |
239 | switch( $month_number ) { |
240 | case '01': |
241 | $ret = _("Jan"); |
242 | break; |
243 | case '02': |
244 | $ret = _("Feb"); |
245 | break; |
246 | case '03': |
247 | $ret = _("Mar"); |
248 | break; |
249 | case '04': |
250 | $ret = _("Apr"); |
251 | break; |
252 | case '05': |
253 | $ret = _("May"); |
254 | break; |
255 | case '06': |
256 | $ret = _("Jun"); |
257 | break; |
258 | case '07': |
259 | $ret = _("Jul"); |
260 | break; |
261 | case '08': |
262 | $ret = _("Aug"); |
263 | break; |
264 | case '09': |
265 | $ret = _("Sep"); |
266 | break; |
267 | case '10': |
268 | $ret = _("Oct"); |
269 | break; |
270 | case '11': |
271 | $ret = _("Nov"); |
272 | break; |
273 | case '12': |
274 | $ret = _("Dec"); |
275 | break; |
276 | default: |
277 | $ret = ''; |
278 | } |
279 | return( $ret ); |
280 | } |
281 | |
8b096f0a |
282 | /** |
283 | * Returns the localized representation of the date/time. |
284 | * |
285 | * @param string date_format The format for the date, like the input for the PHP date() function. |
286 | * @param int stamp the timestamp to convert |
287 | * @return string a full date representation |
288 | */ |
0e4ae91a |
289 | function date_intl( $date_format, $stamp ) { |
e6b76811 |
290 | $ret = str_replace( array('D','F','l','M'), array('$1','$2','$3','$4'), $date_format ); |
8b096f0a |
291 | // to reduce the date calls we retrieve m and w in the same call |
292 | $ret = date('w#m#'. $ret, $stamp ); |
293 | // extract day and month in order to replace later by intl day and month |
c2e29558 |
294 | $aParts = explode('#',$ret); |
26017e66 |
295 | $ret = str_replace(array('$1','$4','$2','$3',), |
296 | array(getDayAbrv($aParts[0]), |
297 | getMonthAbrv($aParts[1]), |
298 | getMonthName($aParts[1]), |
299 | getDayName($aParts[0])), |
300 | $aParts[2]); |
8b096f0a |
301 | return( $ret ); |
0e4ae91a |
302 | } |
d59837cf |
303 | |
8b096f0a |
304 | /** |
305 | * This returns a date of the format "Wed, Oct 29, 2003 9:52 am", |
306 | * or the same in 24H format (depending on the user's settings), |
307 | * and taking localization into accout. |
308 | * |
309 | * @param int stamp the timestamp |
3aaa3214 |
310 | * @param string fallback string to use when stamp not valid |
8b096f0a |
311 | * @return string the long date string |
312 | */ |
3aaa3214 |
313 | function getLongDateString( $stamp, $fallback = '' ) { |
d59837cf |
314 | |
0e4ae91a |
315 | global $hour_format; |
c2e29558 |
316 | |
913ed9a3 |
317 | if ($stamp == -1) { |
3aaa3214 |
318 | return $fallback; |
913ed9a3 |
319 | } |
320 | |
0e4ae91a |
321 | if ( $hour_format == SMPREF_TIME_12HR ) { |
322 | $date_format = _("D, F j, Y g:i a"); |
323 | } else { |
3f7e6af3 |
324 | $date_format = _("D, F j, Y H:i"); |
0e4ae91a |
325 | } |
c2e29558 |
326 | |
0e4ae91a |
327 | return( date_intl( $date_format, $stamp ) ); |
0f1835f3 |
328 | |
0e4ae91a |
329 | } |
d205a7bf |
330 | |
8b096f0a |
331 | /** |
332 | * Returns a short representation of the date, |
333 | * taking timezones and localization into account. |
334 | * Depending on user's settings, this string can be |
335 | * of the form: "14:23" or "Jun 14, 2003" depending |
336 | * on whether the stamp is "today" or not. |
337 | * |
d72549cb |
338 | * @param int $stamp The timestamp |
339 | * @param boolean $return_full_date_and_time When TRUE, |
340 | * ignore all |
341 | * user settings |
342 | * and use full |
343 | * date and time |
344 | * (OPTIONAL; |
345 | * default FALSE) |
8b096f0a |
346 | * @return string the date string |
347 | */ |
d72549cb |
348 | function getDateString( $stamp, $return_full_date_and_time=FALSE ) { |
d68a3926 |
349 | |
26017e66 |
350 | global $invert_time, $hour_format, $show_full_date, $custom_date_format; |
dc06f88c |
351 | |
352 | if ( $stamp == -1 ) { |
353 | return ''; |
354 | } |
c2e29558 |
355 | |
0e4ae91a |
356 | $now = time(); |
c2e29558 |
357 | |
0e4ae91a |
358 | $dateZ = date('Z', $now ); |
3f298968 |
359 | |
360 | // FIXME: isn't this obsolete and introduced as a terrible workaround |
c2e29558 |
361 | // for bugs at other places which are fixed a long time ago? |
0e4ae91a |
362 | if ($invert_time) { |
363 | $dateZ = - $dateZ; |
364 | } |
b0d8461b |
365 | |
366 | // calculate when it was midnight and when it will be, |
367 | // in order to display dates differently if they're 'today' |
0e4ae91a |
368 | $midnight = $now - ($now % 86400) - $dateZ; |
b0d8461b |
369 | // this is to correct if after calculations midnight is more than |
370 | // one whole day away. |
371 | if ($now - $midnight > 86400) { |
372 | $midnight += 86400; |
373 | } |
374 | $nextmid = $midnight + 86400; |
c2e29558 |
375 | |
26017e66 |
376 | $custom_date_format = trim($custom_date_format); |
d72549cb |
377 | |
378 | if ($return_full_date_and_time) { |
379 | if ( $hour_format == SMPREF_TIME_12HR ) { |
316c380a |
380 | $date_format = _("D, F j, Y g:i a"); |
d72549cb |
381 | } else { |
316c380a |
382 | $date_format = _("D, F j, Y H:i"); |
d72549cb |
383 | } |
384 | } else if (!empty($custom_date_format)) { |
26017e66 |
385 | $date_format = $custom_date_format; |
386 | } else if ($show_full_date == 1 || $nextmid < $stamp) { |
3e3b60e3 |
387 | $date_format = _("M j, Y"); |
388 | } else if ($midnight < $stamp) { |
0e4ae91a |
389 | /* Today */ |
390 | if ( $hour_format == SMPREF_TIME_12HR ) { |
391 | $date_format = _("g:i a"); |
392 | } else { |
3f7e6af3 |
393 | $date_format = _("H:i"); |
0e4ae91a |
394 | } |
395 | } else if ($midnight - 518400 < $stamp) { |
396 | /* This week */ |
397 | if ( $hour_format == SMPREF_TIME_12HR ) { |
398 | $date_format = _("D, g:i a"); |
399 | } else { |
3f7e6af3 |
400 | $date_format = _("D, H:i"); |
0e4ae91a |
401 | } |
402 | } else { |
403 | /* before this week */ |
404 | $date_format = _("M j, Y"); |
405 | } |
c2e29558 |
406 | |
0e4ae91a |
407 | return( date_intl( $date_format, $stamp ) ); |
408 | } |
0f1835f3 |
409 | |
8b096f0a |
410 | /** |
411 | * Decodes a RFC 822 Date-header into a timestamp |
412 | * |
413 | * @param array dateParts the Date-header split by whitespace |
414 | * @return int the timestamp calculated from the header |
415 | */ |
0e4ae91a |
416 | function getTimeStamp($dateParts) { |
4f51df66 |
417 | /* $dateParts[0] == <day of week> Mon, Tue, Wed |
418 | * $dateParts[1] == <day of month> 23 |
419 | * $dateParts[2] == <month> Jan, Feb, Mar |
420 | * $dateParts[3] == <year> 1999 |
421 | * $dateParts[4] == <time> 18:54:23 (HH:MM:SS) |
422 | * $dateParts[5] == <from GMT> +0100 |
423 | * $dateParts[6] == <zone> (EDT) |
424 | * |
425 | * NOTE: In RFC 822, it states that <day of week> is optional. |
426 | * In that case, dateParts[0] would be the <day of month> |
427 | * and everything would be bumped up one. |
428 | */ |
d5c4ae7c |
429 | |
1cbd8754 |
430 | if (count($dateParts) <2) { |
2a9b0fad |
431 | return -1; |
1cbd8754 |
432 | } else if (count($dateParts) ==3) { |
c2e29558 |
433 | if (substr_count($dateParts[0],'-') == 2 && |
434 | substr_count($dateParts[1],':') == 2) { |
435 | // dd-Month-yyyy 23:19:05 +0200 |
436 | // redefine the date |
437 | $aDate = explode('-',$dateParts[0]); |
438 | $newDate = array($aDate[0],$aDate[1],$aDate[2],$dateParts[1],$dateParts[2]); |
439 | $dateParts = $newDate; |
440 | } |
1cbd8754 |
441 | } |
3302d0d4 |
442 | |
1cbd8754 |
443 | /* |
444 | * Simply check to see if the first element in the dateParts |
445 | * array is an integer or not. |
446 | * Since the day of week is optional, this check is needed. |
447 | */ |
d5c4ae7c |
448 | if (!is_numeric(trim($dateParts[0]))) { |
1cbd8754 |
449 | /* cope with broken mailers that send "Tue,23" without space */ |
450 | if ( preg_match ('/^\w+,(\d{1,2})$/', $dateParts[0], $match) ) { |
451 | /* replace Tue,23 with 23 */ |
452 | $dateParts[0] = $match[1]; |
453 | } else { |
454 | /* just drop the day of week */ |
455 | array_shift($dateParts); |
456 | } |
0e4ae91a |
457 | } |
1cbd8754 |
458 | |
d5c4ae7c |
459 | /* calculate timestamp separated from the zone and obs-zone */ |
460 | $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4))); |
461 | if (!isset($dateParts[0])) { |
462 | $dateParts[0] = '+0000'; |
0e4ae91a |
463 | } |
dc06f88c |
464 | |
d5c4ae7c |
465 | if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) { |
466 | /* zone in obs-zone format */ |
467 | if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) { |
468 | $obs_zone = $regs[1]; |
469 | } else { |
470 | $obs_zone = $dateParts[0]; |
471 | } |
472 | return getGMTSeconds($stamp, $obs_zone); |
473 | } else { |
474 | return getGMTSeconds($stamp, $dateParts[0]); |
0e4ae91a |
475 | } |
0e4ae91a |
476 | } |