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
11 * @copyright © 1999-2006 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @package squirrelmail
19 * dependency information
24 * Corrects a time stamp to be the local time.
26 * @param int stamp the timestamp to adjust
27 * @param string tzc the timezone correction
28 * @return int the corrected timestamp
30 function getGMTSeconds($stamp, $tzc) {
31 /* date couldn't be parsed */
35 /* timezone correction, expressed as `shhmm' */
83 if (substr($tzc, 0, 1) == '-') {
85 } else if (substr($tzc, 0, 1) != '+') {
88 $hh = substr($tzc,1,2);
89 $mm = substr($tzc,3,2);
90 $iTzc = ($hh * 60 +
$mm) * 60;
91 if ($neg) $iTzc = -1 * (int) $iTzc;
94 /* now find what the server is at */
95 $current = date('Z', time());
96 /* stamp in local timezone */
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
109 * @param int day_number the day number
110 * @return string the day in human readable form
112 function getDayName( $day_number ) {
114 switch( $day_number ) {
125 $ret = _("Wednesday");
128 $ret = _("Thursday");
134 $ret = _("Saturday");
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
147 function getDayAbrv( $day_number ) {
149 switch( $day_number ) {
179 * Returns the (localized) string for a given month number.
181 * @param string month_number the month number (01..12)
182 * @return string the month name in human readable form
184 function getMonthName( $month_number ) {
185 switch( $month_number ) {
190 $ret = _("February");
211 $ret = _("September");
217 $ret = _("November");
220 $ret = _("December");
229 * Returns the (localized) string for a given month number,
230 * short representation.
232 * @param string month_number the month number (01..12)
233 * @return string the shortened month in human readable form
235 function getMonthAbrv( $month_number ) {
236 switch( $month_number ) {
250 $ret = _("May");
280 * Returns the localized representation of the date/time.
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
286 function date_intl( $date_format, $stamp ) {
287 $ret = str_replace( array('D','F','l','M'), array('$1','$2','$3','$4'), $date_format );
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
291 $aParts = explode('#',$ret);
292 $ret = str_replace(array('$1','$4','$2','$3',), array(getDayAbrv($aParts[0]),
293 getMonthAbrv($aParts[1]),
294 getMonthName($aParts[1]),
295 getDayName($aParts[0])),
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.
305 * @param int stamp the timestamp
306 * @return string the long date string
308 function getLongDateString( $stamp ) {
316 if ( $hour_format == SMPREF_TIME_12HR
) {
317 $date_format = _("D, F j, Y g:i a");
319 $date_format = _("D, F j, Y H:i");
322 return( date_intl( $date_format, $stamp ) );
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.
333 * @param int stamp the timestamp
334 * @return string the date string
336 function getDateString( $stamp ) {
338 global $invert_time, $hour_format, $show_full_date;
340 if ( $stamp == -1 ) {
346 $dateZ = date('Z', $now );
348 // FIXME: isn't this obsolete and introduced as a terrible workaround
349 // for bugs at other places which are fixed a long time ago?
354 // calculate when it was midnight and when it will be,
355 // in order to display dates differently if they're 'today'
356 $midnight = $now - ($now %
86400) - $dateZ;
357 // this is to correct if after calculations midnight is more than
358 // one whole day away.
359 if ($now - $midnight > 86400) {
362 $nextmid = $midnight +
86400;
364 if (($show_full_date == 1) ||
($nextmid < $stamp)) {
365 $date_format = _("M j, Y");
366 } else if ($midnight < $stamp) {
368 if ( $hour_format == SMPREF_TIME_12HR
) {
369 $date_format = _("g:i a");
371 $date_format = _("H:i");
373 } else if ($midnight - 518400 < $stamp) {
375 if ( $hour_format == SMPREF_TIME_12HR
) {
376 $date_format = _("D, g:i a");
378 $date_format = _("D, H:i");
381 /* before this week */
382 $date_format = _("M j, Y");
385 return( date_intl( $date_format, $stamp ) );
389 * Decodes a RFC 822 Date-header into a timestamp
391 * @param array dateParts the Date-header split by whitespace
392 * @return int the timestamp calculated from the header
394 function getTimeStamp($dateParts) {
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)
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.
409 * Simply check to see if the first element in the dateParts
410 * array is an integer or not.
411 * Since the day of week is optional, this check is needed.
413 if (count($dateParts) <2) {
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
420 $aDate = explode('-',$dateParts[0]);
421 $newDate = array($aDate[0],$aDate[1],$aDate[2],$dateParts[1],$dateParts[2]);
422 $dateParts = $newDate;
426 /* remove day of week */
427 if (!is_numeric(trim($dateParts[0]))) {
428 $dataParts = array_shift($dateParts);
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';
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];
441 $obs_zone = $dateParts[0];
443 return getGMTSeconds($stamp, $obs_zone);
445 return getGMTSeconds($stamp, $dateParts[0]);