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-2007 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' */
85 if (substr($tzc, 0, 1) == '-') {
87 } else if (substr($tzc, 0, 1) != '+') {
90 $hh = substr($tzc,1,2);
91 $mm = substr($tzc,3,2);
92 $iTzc = ($hh * 60 +
$mm) * 60;
93 if ($neg) $iTzc = -1 * (int) $iTzc;
96 /* now find what the server is at */
97 $current = date('Z', time());
98 /* stamp in local timezone */
105 * Returns the (localized) string for a given day number.
106 * Switch system has been intentionaly chosen for the
107 * internationalization of month and day names. The reason
108 * is to make sure that _("") strings will go into the
111 * @param int day_number the day number
112 * @return string the day in human readable form
114 function getDayName( $day_number ) {
116 switch( $day_number ) {
127 $ret = _("Wednesday");
130 $ret = _("Thursday");
136 $ret = _("Saturday");
145 * Like getDayName, but returns the short form
146 * @param int day_number the day number
147 * @return string the day in short human readable form
149 function getDayAbrv( $day_number ) {
151 switch( $day_number ) {
181 * Returns the (localized) string for a given month number.
183 * @param string month_number the month number (01..12)
184 * @return string the month name in human readable form
186 function getMonthName( $month_number ) {
187 switch( $month_number ) {
192 $ret = _("February");
213 $ret = _("September");
219 $ret = _("November");
222 $ret = _("December");
231 * Returns the (localized) string for a given month number,
232 * short representation.
234 * @param string month_number the month number (01..12)
235 * @return string the shortened month in human readable form
237 function getMonthAbrv( $month_number ) {
238 switch( $month_number ) {
252 $ret = _("May");
282 * Returns the localized representation of the date/time.
284 * @param string date_format The format for the date, like the input for the PHP date() function.
285 * @param int stamp the timestamp to convert
286 * @return string a full date representation
288 function date_intl( $date_format, $stamp ) {
289 $ret = str_replace( array('D','F','l','M'), array('$1','$2','$3','$4'), $date_format );
290 // to reduce the date calls we retrieve m and w in the same call
291 $ret = date('w#m#'. $ret, $stamp );
292 // extract day and month in order to replace later by intl day and month
293 $aParts = explode('#',$ret);
294 $ret = str_replace(array('$1','$4','$2','$3',),
295 array(getDayAbrv($aParts[0]),
296 getMonthAbrv($aParts[1]),
297 getMonthName($aParts[1]),
298 getDayName($aParts[0])),
304 * This returns a date of the format "Wed, Oct 29, 2003 9:52 am",
305 * or the same in 24H format (depending on the user's settings),
306 * and taking localization into accout.
308 * @param int stamp the timestamp
309 * @param string fallback string to use when stamp not valid
310 * @return string the long date string
312 function getLongDateString( $stamp, $fallback = '' ) {
320 if ( $hour_format == SMPREF_TIME_12HR
) {
321 $date_format = _("D, F j, Y g:i a");
323 $date_format = _("D, F j, Y H:i");
326 return( date_intl( $date_format, $stamp ) );
331 * Returns a short representation of the date,
332 * taking timezones and localization into account.
333 * Depending on user's settings, this string can be
334 * of the form: "14:23" or "Jun 14, 2003" depending
335 * on whether the stamp is "today" or not.
337 * @param int stamp the timestamp
338 * @return string the date string
340 function getDateString( $stamp ) {
342 global $invert_time, $hour_format, $show_full_date, $custom_date_format;
344 if ( $stamp == -1 ) {
350 $dateZ = date('Z', $now );
352 // FIXME: isn't this obsolete and introduced as a terrible workaround
353 // for bugs at other places which are fixed a long time ago?
358 // calculate when it was midnight and when it will be,
359 // in order to display dates differently if they're 'today'
360 $midnight = $now - ($now %
86400) - $dateZ;
361 // this is to correct if after calculations midnight is more than
362 // one whole day away.
363 if ($now - $midnight > 86400) {
366 $nextmid = $midnight +
86400;
368 $custom_date_format = trim($custom_date_format);
369 if (!empty($custom_date_format)) {
370 $date_format = $custom_date_format;
371 } else if ($show_full_date == 1 ||
$nextmid < $stamp) {
372 $date_format = _("M j, Y");
373 } else if ($midnight < $stamp) {
375 if ( $hour_format == SMPREF_TIME_12HR
) {
376 $date_format = _("g:i a");
378 $date_format = _("H:i");
380 } else if ($midnight - 518400 < $stamp) {
382 if ( $hour_format == SMPREF_TIME_12HR
) {
383 $date_format = _("D, g:i a");
385 $date_format = _("D, H:i");
388 /* before this week */
389 $date_format = _("M j, Y");
392 return( date_intl( $date_format, $stamp ) );
396 * Decodes a RFC 822 Date-header into a timestamp
398 * @param array dateParts the Date-header split by whitespace
399 * @return int the timestamp calculated from the header
401 function getTimeStamp($dateParts) {
402 /* $dateParts[0] == <day of week> Mon, Tue, Wed
403 * $dateParts[1] == <day of month> 23
404 * $dateParts[2] == <month> Jan, Feb, Mar
405 * $dateParts[3] == <year> 1999
406 * $dateParts[4] == <time> 18:54:23 (HH:MM:SS)
407 * $dateParts[5] == <from GMT> +0100
408 * $dateParts[6] == <zone> (EDT)
410 * NOTE: In RFC 822, it states that <day of week> is optional.
411 * In that case, dateParts[0] would be the <day of month>
412 * and everything would be bumped up one.
415 if (count($dateParts) <2) {
417 } else if (count($dateParts) ==3) {
418 if (substr_count($dateParts[0],'-') == 2 &&
419 substr_count($dateParts[1],':') == 2) {
420 // dd-Month-yyyy 23:19:05 +0200
422 $aDate = explode('-',$dateParts[0]);
423 $newDate = array($aDate[0],$aDate[1],$aDate[2],$dateParts[1],$dateParts[2]);
424 $dateParts = $newDate;
429 * Simply check to see if the first element in the dateParts
430 * array is an integer or not.
431 * Since the day of week is optional, this check is needed.
433 if (!is_numeric(trim($dateParts[0]))) {
434 /* cope with broken mailers that send "Tue,23" without space */
435 if ( preg_match ('/^\w+,(\d{1,2})$/', $dateParts[0], $match) ) {
436 /* replace Tue,23 with 23 */
437 $dateParts[0] = $match[1];
439 /* just drop the day of week */
440 array_shift($dateParts);
444 /* calculate timestamp separated from the zone and obs-zone */
445 $stamp = strtotime(implode (' ', array_splice ($dateParts,0,4)));
446 if (!isset($dateParts[0])) {
447 $dateParts[0] = '+0000';
450 if (!preg_match('/^[+-]{1}[0-9]{4}$/',$dateParts[0])) {
451 /* zone in obs-zone format */
452 if (preg_match('/\((.+)\)/',$dateParts[0],$regs)) {
453 $obs_zone = $regs[1];
455 $obs_zone = $dateParts[0];
457 return getGMTSeconds($stamp, $obs_zone);
459 return getGMTSeconds($stamp, $dateParts[0]);