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