Esempio n. 1
0
 /**
  * Format a date
  *
  * Accept either a time stamp, or a formatted string as input parameter:
  * - YYYY-MM-DD HH:MM:SS
  * - YYMMDD HH:MM:SS GMT
  *
  * The variant is processed as follows:
  * - 'day' - only day, month and year --no time information
  * - 'with_hour' - adapts to time scale, and mention hours for today and yesterday
  * - 'no_hour' - adapts to time scale, but don't mention hours
  * - 'full' - display the full date
  * - 'month' - only month and year
  * - 'publishing' - allows for smart skinning
  * - 'standalone' - like full, but without the 'on ' prefix
  * - 'iso8601' - special format
  * - 'plain' - example 'Feb 26 2010 22:30:31 GMT'
  * - 'yyyy-mm-dd' - only day, month and year --no time information
  *
  * The date provided is considered to be GMT-based.
  * It is adjusted to the time zone of the surfer, if applicable.
  * This adjustment does not apply to following variants:
  * - 'day'
  * - 'iso8601'
  * - 'standalone'
  *
  *
  * @link http://www.w3.org/TR/NOTE-datetime the w3c profile of ISO 8601
  * @link http://www.cs.tut.fi/~jkorpela/iso8601.html short description of ISO 8601
  * @link http://fr2.php.net/date Code for ISO 8601 formatting
  *
  * @param int or string the date to be displayed
  * @param string the variant
  * @param string the language to express this stamp
  * @param int offset to GMT of the provided date, if any
  * @return the HTML to be used
  */
 public static function &build_date($stamp, $variant = 'with_hour', $language = NULL, $gmt_offset = 0)
 {
     global $context, $local;
     // return by reference
     $output = '';
     // sanity check
     if (!isset($stamp) || !$stamp) {
         return $output;
     }
     // surfer offset, except on 'day' and 'iso8601'
     if ($variant == 'day' || $variant == 'iso8601' || $variant == 'standalone' || $variant == 'local') {
         $surfer_offset = 0;
     } else {
         $surfer_offset = Surfer::get_gmt_offset();
     }
     // YYMMDD-HH:MM:SS GMT -- this one is natively GMT
     if (preg_match('/GMT$/', $stamp) && strlen($stamp) == 19) {
         // YYMMDD-HH:MM:SS GMT -> HH, MM, SS, MM, DD, YY
         $actual_stamp = mktime(substr($stamp, 7, 2), substr($stamp, 10, 2), substr($stamp, 13, 2), substr($stamp, 2, 2), substr($stamp, 4, 2), substr($stamp, 0, 2));
         // adjust to surfer time zone
         $actual_stamp += $surfer_offset * 3600;
         // time()-like stamp
     } elseif (intval($stamp) > 10000000) {
         // adjust to surfer time zone
         $actual_stamp = intval($stamp) + ($surfer_offset - $gmt_offset) * 3600;
         // YYYY-MM-DD HH:MM:SS, or a string that can be readed
     } elseif (($actual_stamp = strtotime($stamp)) != -1) {
         // adjust to surfer time zone
         $actual_stamp += ($surfer_offset - $gmt_offset) * 3600;
     } else {
         $output = '*' . $stamp . '*';
         return $output;
     }
     if (!($items = @getdate($actual_stamp))) {
         $output = '*' . $stamp . '*';
         return $output;
     }
     // if undefined language, use preferred language for absolute formats
     if ($language) {
     } elseif ($variant == 'full' || $variant == 'iso8601' || $variant == 'local') {
         $language = $context['preferred_language'];
     } else {
         $language = $context['language'];
     }
     // in French '1' -> '1er'
     if ($language == 'fr' && $items['mday'] == 1) {
         $items['mday'] = '1er';
     }
     // allow for months localization through i18n
     $months = array('*', i18n::s('Jan.'), i18n::s('Feb.'), i18n::s('Mar.'), i18n::s('Apr.'), i18n::s('May'), i18n::s('June'), i18n::s('Jul.'), i18n::s('Aug.'), i18n::s('Sep.'), i18n::s('Oct.'), i18n::s('Nov.'), i18n::s('Dec.'));
     // load month labels adapted to required language
     $months = array('*', i18n::lookup($context['l10n'][$language], 'Jan.'), i18n::lookup($context['l10n'][$language], 'Feb.'), i18n::lookup($context['l10n'][$language], 'Mar.'), i18n::lookup($context['l10n'][$language], 'Apr.'), i18n::lookup($context['l10n'][$language], 'May'), i18n::lookup($context['l10n'][$language], 'June'), i18n::lookup($context['l10n'][$language], 'Jul.'), i18n::lookup($context['l10n'][$language], 'Aug.'), i18n::lookup($context['l10n'][$language], 'Sep.'), i18n::lookup($context['l10n'][$language], 'Oct.'), i18n::lookup($context['l10n'][$language], 'Nov.'), i18n::lookup($context['l10n'][$language], 'Dec.'));
     // now
     $now = time();
     // server actual offset -- provided as a parameter
     //		$gmt_offset = intval((strtotime(date('M d Y H:i:s')) - strtotime(gmdate('M d Y H:i:s'))) / 3600);
     // server stamp, as seen by surfer
     $today = getdate($now + ($surfer_offset - $gmt_offset) * 3600);
     // time stamp only
     if (preg_match('/00:00/', date('H:i', $actual_stamp))) {
         $time = '';
     } else {
         $trailer = '';
         if (!$surfer_offset && $variant !== 'local') {
             $trailer = ' GMT';
         }
         $time = sprintf(i18n::s(' at %s%s'), date(i18n::s('h:i a'), $actual_stamp), $trailer);
     }
     // format a date as an absolute string
     if ($variant == 'full' || $variant == 'local') {
         if ($language == 'fr') {
             $output .= $items['mday'] . ' ' . $months[$items['mon']] . ' ' . $items['year'] . $time;
         } else {
             $output .= $months[$items['mon']] . ' ' . $items['mday'] . ' ' . $items['year'] . $time;
         }
         return $output;
     }
     // publishing date is in the future
     if ($variant == 'publishing' && strcmp($stamp, $context['now']) > 0) {
         $output .= DRAFT_FLAG;
     }
     // allow for smart skinning -- http://www.wplover.com/1449/easier-date-display-technique-with-css-3/
     if ($variant == 'publishing') {
         $output .= '<span class="day">' . $items['mday'] . '</span>' . '<span class="month">' . $months[$items['mon']] . '</span>' . '<span class="year">' . $items['year'] . '</span>';
         return $output;
     }
     // the same, but without prefix
     if ($variant == 'standalone') {
         if ($language == 'fr') {
             $output .= $items['mday'] . ' ' . $months[$items['mon']] . ' ' . $items['year'] . $time;
         } else {
             $output .= $months[$items['mon']] . ' ' . $items['mday'] . ' ' . $items['year'] . $time;
         }
         return $output;
     }
     // month only
     if ($variant == 'month') {
         if ($language == 'fr') {
             $output .= $months[$items['mon']] . ' ' . $items['year'];
         } else {
             $output .= $months[$items['mon']] . ' ' . $items['year'];
         }
         return $output;
     }
     // day only
     if ($variant == 'day') {
         // same year, don't mention it
         if ($items['year'] == $today['year']) {
             if ($language == 'fr') {
                 $output .= $items['mday'] . ' ' . $months[$items['mon']];
             } else {
                 $output .= $months[$items['mon']] . ' ' . $items['mday'];
             }
             // different year
         } else {
             if ($language == 'fr') {
                 $output .= $items['mday'] . ' ' . $months[$items['mon']] . ' ' . $items['year'];
             } else {
                 $output .= $months[$items['mon']] . ' ' . $items['mday'] . ' ' . $items['year'];
             }
         }
         return $output;
     }
     // in a calendar
     if ($variant == 'calendar') {
         include_once $context['path_to_root'] . 'dates/dates.php';
         $month_link =& Skin::build_link(Dates::get_url($items['year'] . '/' . $items['mon'], 'month'), $months[$items['mon']], 'basic', i18n::s('Calendar of this month'));
         $year_link =& Skin::build_link(Dates::get_url($items['year'], 'year'), $items['year'], 'basic', i18n::s('Calendar of this year'));
         if ($language == 'fr') {
             $output .= $items['mday'] . ' ' . $month_link . ' ' . $year_link;
         } else {
             $output .= $month_link . ' ' . $items['mday'] . ' ' . $year_link;
         }
         return $output;
     }
     // format a date according to ISO 8601 format
     if ($variant == 'iso8601') {
         $tzd = date('O', $actual_stamp);
         $tzd = $tzd[0] . str_pad((int) ($tzd / 100), 2, "0", STR_PAD_LEFT) . ':' . str_pad((int) ($tzd % 100), 2, "0", STR_PAD_LEFT);
         $output = date('Y-m-d\\TH:i:s', $actual_stamp) . $tzd;
         return $output;
     }
     // format a raw date
     if ($variant == 'yyyy-mm-dd') {
         $output = date('Y-m-d', $actual_stamp);
         return $output;
     }
     // plain date
     if ($variant == 'plain') {
         $output = date('M d Y H:i:s', $actual_stamp) . ' GMT';
         return $output;
     }
     // less than 24 hours
     if ($items['yday'] == $today['yday'] && $items['year'] == $today['year']) {
         $time = date('H:i', $actual_stamp);
         if ($variant == 'no_hour' || preg_match('/00:00/', $time)) {
             $local['today_en'] = 'today';
             $local['today_fr'] = 'aujourd\'hui';
         } else {
             $trailer = '';
             if (!$surfer_offset) {
                 $trailer = ' GMT';
             }
             $local['today_en'] = 'today at ' . date('h:i a', $actual_stamp) . $trailer;
             $local['today_fr'] = 'aujourd\'hui &agrave; ' . date('H:i', $actual_stamp) . $trailer;
         }
         $output = i18n::user('today');
         return $output;
         // less than 48 hours
     } elseif ($items['yday'] + 1 == $today['yday'] && $items['year'] == $today['year']) {
         $time = date('H:i', $actual_stamp);
         if ($variant == 'no_hour' || preg_match('/00:00/', $time)) {
             $local['yesterday_en'] = 'yesterday';
             $local['yesterday_fr'] = 'hier';
         } else {
             $trailer = '';
             if (!$surfer_offset) {
                 $trailer = ' GMT';
             }
             $local['yesterday_en'] = 'yesterday at ' . date('h:i a', $actual_stamp) . $trailer;
             $local['yesterday_fr'] = 'hier &agrave; ' . date('H:i', $actual_stamp) . $trailer;
         }
         $output = i18n::user('yesterday');
         return $output;
         // this year
     } elseif ($stamp <= $now && $items['year'] == $today['year']) {
         if ($language == 'fr') {
             $output .= 'le ' . $items['mday'] . ' ' . $months[$items['mon']];
         } else {
             $output .= 'on ' . $months[$items['mon']] . ' ' . $items['mday'];
         }
         return $output;
         // date in fr: le dd mmm yy
     } elseif ($language == 'fr') {
         $output .= 'le ' . $items['mday'] . ' ' . $months[$items['mon']] . ' ' . $items['year'];
         // date in en: on mmm dd yy
     } else {
         $output .= 'on ' . $months[$items['mon']] . ' ' . $items['mday'] . ' ' . $items['year'];
     }
     $output .= $time ? ' ' . $time : '';
     return $output;
 }
Esempio n. 2
0
File: i18n.php Progetto: rair/yacs
 /**
  * get a localized string for a surfer
  *
  * This function is an equivalent to gettext(), except it deals with surfer localization.
  *
  * @param string the template string to be translated
  * @return string the localized string, if any
  */
 public static function &s($text)
 {
     global $context;
     // sanity check
     if (!$text) {
         return $text;
     }
     // select language used by surfer
     if (isset($context['language'])) {
         $locale = $context['language'];
     } else {
         $locale = 'en';
     }
     // cache is empty
     if (!isset($context['l10n']) || !isset($context['l10n'][$locale]) || !is_array($context['l10n'][$locale])) {
         return $text;
     }
     // provide the localized string
     $text =& i18n::lookup($context['l10n'][$locale], $text);
     return $text;
 }