Remove random default argument value in the middle of argument list
[squirrelmail.git] / functions / date.php
1 <?php
2
3 /**
4 * date.php
5 *
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 *
11 * @copyright 1999-2021 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @version $Id$
14 * @package squirrelmail
15 * @subpackage date
16 */
17
18 /**
19 * dependency information
20 * - none
21 */
22
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 */
30 function getGMTSeconds($stamp, $tzc) {
31 /* date couldn't be parsed */
32 if ($stamp == -1) {
33 return -1;
34 }
35 /* timezone correction, expressed as `shhmm' */
36 switch($tzc)
37 {
38 case 'Pacific':
39 case 'PST':
40 $tzc = '-0800';
41 break;
42 case 'Mountain':
43 case 'MST':
44 case 'PDT':
45 $tzc = '-0700';
46 break;
47 case 'Central':
48 case 'CST':
49 case 'MDT':
50 $tzc = '-0600';
51 break;
52 case 'Eastern':
53 case 'EST':
54 case 'CDT':
55 $tzc = '-0500';
56 break;
57 case 'EDT':
58 $tzc = '-0400';
59 break;
60 case 'GMT':
61 case 'UTC':
62 $tzc = '+0000';
63 break;
64 case 'BST':
65 case 'MET':
66 case 'CET':
67 $tzc = '+0100';
68 break;
69 case 'EET':
70 case 'IST':
71 case 'MET DST':
72 case 'METDST':
73 case 'MEST':
74 case 'CEST':
75 $tzc = '+0200';
76 break;
77 case 'HKT':
78 $tzc = '+0800';
79 break;
80 case 'JST':
81 case 'KST':
82 $tzc = '+0900';
83 break;
84 }
85 $neg = false;
86 if (substr($tzc, 0, 1) == '-') {
87 $neg = true;
88 } else if (substr($tzc, 0, 1) != '+') {
89 $tzc = '+'.$tzc;
90 }
91 $hh = substr($tzc,1,2);
92 $mm = substr($tzc,3,2);
93 $iTzc = ($hh * 60 + $mm) * 60;
94 if ($neg) $iTzc = -1 * (int) $iTzc;
95 /* stamp in gmt */
96 $stamp -= $iTzc;
97 /* now find what the server is at */
98 $current = date('Z', time());
99 /* stamp in local timezone */
100 $stamp += $current;
101
102 return $stamp;
103 }
104
105 /**
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 */
115 function getDayName( $day_number ) {
116
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 }
144
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 */
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
180
181 /**
182 * Returns the (localized) string for a given month number.
183 *
184 * @param string month_number the month number (01..12)
185 * @return string the month name in human readable form
186 */
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 }
230
231 /**
232 * Returns the (localized) string for a given month number,
233 * short representation.
234 *
235 * @param string month_number the month number (01..12)
236 * @return string the shortened month in human readable form
237 */
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 = _("Ma&#121;");
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
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 */
289 function date_intl( $date_format, $stamp ) {
290 $ret = str_replace( array('D','F','l','M'), array('$1','$2','$3','$4'), $date_format );
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
294 $aParts = explode('#',$ret);
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]);
301 return( $ret );
302 }
303
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
310 * @param string fallback string to use when stamp not valid
311 * @return string the long date string
312 */
313 function getLongDateString( $stamp, $fallback = '' ) {
314
315 global $hour_format;
316
317 if ($stamp == -1) {
318 return $fallback;
319 }
320
321 if ( $hour_format == SMPREF_TIME_12HR ) {
322 $date_format = _("D, F j, Y g:i a");
323 } else {
324 $date_format = _("D, F j, Y H:i");
325 }
326
327 return( date_intl( $date_format, $stamp ) );
328
329 }
330
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 *
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)
346 * @return string the date string
347 */
348 function getDateString( $stamp, $return_full_date_and_time=FALSE ) {
349
350 global $invert_time, $hour_format, $show_full_date, $custom_date_format;
351
352 if ( $stamp == -1 ) {
353 return '';
354 }
355
356 $now = time();
357
358 $dateZ = date('Z', $now );
359
360 // FIXME: isn't this obsolete and introduced as a terrible workaround
361 // for bugs at other places which are fixed a long time ago?
362 if ($invert_time) {
363 $dateZ = - $dateZ;
364 }
365
366 // calculate when it was midnight and when it will be,
367 // in order to display dates differently if they're 'today'
368 $midnight = $now - ($now % 86400) - $dateZ;
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;
375
376 $custom_date_format = trim($custom_date_format);
377
378 if ($return_full_date_and_time) {
379 if ( $hour_format == SMPREF_TIME_12HR ) {
380 $date_format = _("D, F j, Y g:i a");
381 } else {
382 $date_format = _("D, F j, Y H:i");
383 }
384 } else if (!empty($custom_date_format)) {
385 $date_format = $custom_date_format;
386 } else if ($show_full_date == 1 || $nextmid < $stamp) {
387 $date_format = _("M j, Y");
388 } else if ($midnight < $stamp) {
389 /* Today */
390 if ( $hour_format == SMPREF_TIME_12HR ) {
391 $date_format = _("g:i a");
392 } else {
393 $date_format = _("H:i");
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 {
400 $date_format = _("D, H:i");
401 }
402 } else {
403 /* before this week */
404 $date_format = _("M j, Y");
405 }
406
407 return( date_intl( $date_format, $stamp ) );
408 }
409
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 */
416 function getTimeStamp($dateParts) {
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 */
429
430 if (count($dateParts) <2) {
431 return -1;
432 } else if (count($dateParts) ==3) {
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 }
441 }
442
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 */
448 if (!is_numeric(trim($dateParts[0]))) {
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 }
457 }
458
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';
463 }
464
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]);
475 }
476 }