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