diff options
Diffstat (limited to 'phpBB/includes/user.php')
-rw-r--r-- | phpBB/includes/user.php | 94 |
1 files changed, 30 insertions, 64 deletions
diff --git a/phpBB/includes/user.php b/phpBB/includes/user.php index ce9c804f23..4c62dd93d7 100644 --- a/phpBB/includes/user.php +++ b/phpBB/includes/user.php @@ -29,8 +29,7 @@ class phpbb_user extends phpbb_session var $help = array(); var $theme = array(); var $date_format; - var $timezone; - var $dst; + public $tz; var $lang_name = false; var $lang_id = false; @@ -79,15 +78,13 @@ class phpbb_user extends phpbb_session $this->lang_name = (file_exists($this->lang_path . $this->data['user_lang'] . "/common.$phpEx")) ? $this->data['user_lang'] : basename($config['default_lang']); $this->date_format = $this->data['user_dateformat']; - $this->timezone = $this->data['user_timezone'] * 3600; - $this->dst = $this->data['user_dst'] * 3600; + $this->tz = $this->data['user_timezone']; } else { $this->lang_name = basename($config['default_lang']); $this->date_format = $config['default_dateformat']; - $this->timezone = $config['board_timezone'] * 3600; - $this->dst = $config['board_dst'] * 3600; + $this->tz = $config['board_timezone']; /** * If a guest user is surfing, we try to guess his/her language first by obtaining the browser language @@ -126,6 +123,14 @@ class phpbb_user extends phpbb_session */ } + if (is_numeric($this->tz)) + { + // Might still be numeric by chance + $this->tz = sprintf('Etc/GMT%+d', ($this->tz + ($this->data['user_id'] != ANONYMOUS ? $this->data['user_dst'] : $config['board_dst']))); + } + + $this->tz = new DateTimeZone($this->tz); + // We include common language file here to not load it every time a custom language file is included $lang = &$this->lang; @@ -615,70 +620,31 @@ class phpbb_user extends phpbb_session */ function format_date($gmepoch, $format = false, $forcedate = false) { - static $midnight; - static $date_cache; - - $format = (!$format) ? $this->date_format : $format; - $now = time(); - $delta = $now - $gmepoch; - - if (!isset($date_cache[$format])) - { - // Is the user requesting a friendly date format (i.e. 'Today 12:42')? - $date_cache[$format] = array( - 'is_short' => strpos($format, '|'), - 'format_short' => substr($format, 0, strpos($format, '|')) . '||' . substr(strrchr($format, '|'), 1), - 'format_long' => str_replace('|', '', $format), - 'lang' => $this->lang['datetime'], - ); - - // Short representation of month in format? Some languages use different terms for the long and short format of May - if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false)) - { - $date_cache[$format]['lang']['May'] = $this->lang['datetime']['May_short']; - } - } - - // Zone offset - $zone_offset = $this->timezone + $this->dst; + static $utc; - // Show date < 1 hour ago as 'xx min ago' but not greater than 60 seconds in the future - // A small tolerence is given for times in the future but in the same minute are displayed as '< than a minute ago' - if ($delta < 3600 && $delta > -60 && ($delta >= -5 || (($now / 60) % 60) == (($gmepoch / 60) % 60)) && $date_cache[$format]['is_short'] !== false && !$forcedate && isset($this->lang['datetime']['AGO'])) + if (!isset($utc)) { - return $this->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60))); + $utc = new DateTimeZone('UTC'); } - if (!$midnight) - { - list($d, $m, $y) = explode(' ', gmdate('j n Y', time() + $zone_offset)); - $midnight = gmmktime(0, 0, 0, $m, $d, $y) - $zone_offset; - } - - if ($date_cache[$format]['is_short'] !== false && !$forcedate && !($gmepoch < $midnight - 86400 || $gmepoch > $midnight + 172800)) - { - $day = false; + $time = new phpbb_datetime("@$gmepoch", $utc, $this); + $time->setTimezone($this->tz); - if ($gmepoch > $midnight + 86400) - { - $day = 'TOMORROW'; - } - else if ($gmepoch > $midnight) - { - $day = 'TODAY'; - } - else if ($gmepoch > $midnight - 86400) - { - $day = 'YESTERDAY'; - } - - if ($day !== false) - { - return str_replace('||', $this->lang['datetime'][$day], strtr(@gmdate($date_cache[$format]['format_short'], $gmepoch + $zone_offset), $date_cache[$format]['lang'])); - } - } + return $time->format($format, $forcedate); + } - return strtr(@gmdate($date_cache[$format]['format_long'], $gmepoch + $zone_offset), $date_cache[$format]['lang']); + /** + * Create a phpbb_datetime object in the context of the current user + * + * @since 3.1 + * @param string $time String in a format accepted by strtotime(). + * @param DateTimeZone $timezone Time zone of the time. + * @return phpbb_datetime Date time object linked to the current users locale + */ + public function create_datetime($time = 'now', DateTimeZone $timezone = null) + { + $timezone = $timezone ?: $this->tz; + return new phpbb_datetime($time, $timezone, $this); } /** |