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-2020 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' */
86 if (substr($tzc, 0, 1) == '-') {
88 } else if (substr($tzc, 0, 1) != '+') {
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;
97 /* now find what the server is at */
98 $current = date('Z', time());
99 /* stamp in local timezone */
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
112 * @param int day_number the day number
113 * @return string the day in human readable form
115 function getDayName( $day_number ) {
117 switch( $day_number ) {
128 $ret = _("Wednesday");
131 $ret = _("Thursday");
137 $ret = _("Saturday");
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
150 function getDayAbrv( $day_number ) {
152 switch( $day_number ) {
182 * Returns the (localized) string for a given month number.
184 * @param string month_number the month number (01..12)
185 * @return string the month name in human readable form
187 function getMonthName( $month_number ) {
188 switch( $month_number ) {
193 $ret = _("February");
214 $ret = _("September");
220 $ret = _("November");
223 $ret = _("December");
232 * Returns the (localized) string for a given month number,
233 * short representation.
235 * @param string month_number the month number (01..12)
236 * @return string the shortened month in human readable form
238 function getMonthAbrv( $month_number ) {
239 switch( $month_number ) {
253 $ret = _("May");
283 * Returns the localized representation of the date/time.
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
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])),
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.
309 * @param int stamp the timestamp
310 * @param string fallback string to use when stamp not valid
311 * @return string the long date string
313 function getLongDateString( $stamp, $fallback = '' ) {
321 if ( $hour_format == SMPREF_TIME_12HR
) {
322 $date_format = _("D, F j, Y g:i a");
324 $date_format = _("D, F j, Y H:i");
327 return( date_intl( $date_format, $stamp ) );
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.
338 * @param int $stamp The timestamp
339 * @param boolean $return_full_date_and_time When TRUE,
346 * @return string the date string
348 function getDateString( $stamp, $return_full_date_and_time=FALSE ) {
350 global $invert_time, $hour_format, $show_full_date, $custom_date_format;
352 if ( $stamp == -1 ) {
358 $dateZ = date('Z', $now );
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?
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) {
374 $nextmid = $midnight +
86400;
376 $custom_date_format = trim($custom_date_format);
378 if ($return_full_date_and_time) {
379 if ( $hour_format == SMPREF_TIME_12HR
) {
380 $date_format = _("D, F j, Y g:i a");
382 $date_format = _("D, F j, Y H:i");
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) {
390 if ( $hour_format == SMPREF_TIME_12HR
) {
391 $date_format = _("g:i a");
393 $date_format = _("H:i");
395 } else if ($midnight - 518400 < $stamp) {
397 if ( $hour_format == SMPREF_TIME_12HR
) {
398 $date_format = _("D, g:i a");
400 $date_format = _("D, H:i");
403 /* before this week */
404 $date_format = _("M j, Y");
407 return( date_intl( $date_format, $stamp ) );
411 * Decodes a RFC 822 Date-header into a timestamp
413 * @param array dateParts the Date-header split by whitespace
414 * @return int the timestamp calculated from the header
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)
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.
430 if (count($dateParts) <2) {
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
437 $aDate = explode('-',$dateParts[0]);
438 $newDate = array($aDate[0],$aDate[1],$aDate[2],$dateParts[1],$dateParts[2]);
439 $dateParts = $newDate;
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.
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];
454 /* just drop the day of week */
455 array_shift($dateParts);
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';
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];
470 $obs_zone = $dateParts[0];
472 return getGMTSeconds($stamp, $obs_zone);
474 return getGMTSeconds($stamp, $dateParts[0]);