diff options
Diffstat (limited to 'phpBB/includes/user.php')
| -rw-r--r-- | phpBB/includes/user.php | 113 | 
1 files changed, 50 insertions, 63 deletions
| diff --git a/phpBB/includes/user.php b/phpBB/includes/user.php index cf9e6b9994..fcbfaaddfa 100644 --- a/phpBB/includes/user.php +++ b/phpBB/includes/user.php @@ -29,8 +29,11 @@ class phpbb_user extends phpbb_session  	var $help = array();  	var $style = array();  	var $date_format; -	var $timezone; -	var $dst; + +	/** +	* DateTimeZone object holding the timezone of the user +	*/ +	public $timezone;  	var $lang_name = false;  	var $lang_id = false; @@ -79,15 +82,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; +			$user_timezone = $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; +			$user_timezone = $config['board_timezone'];  			/**  			* If a guest user is surfing, we try to guess his/her language first by obtaining the browser language @@ -126,6 +127,16 @@ class phpbb_user extends phpbb_session  			*/  		} +		try +		{ +			$this->timezone = new DateTimeZone($user_timezone); +		} +		catch (Exception $e) +		{ +			// If the timezone the user has selected is invalid, we fall back to UTC. +			$this->timezone = new DateTimeZone('UTC'); +		} +  		// We include common language file here to not load it every time a custom language file is included  		$lang = &$this->lang; @@ -612,70 +623,46 @@ 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']; -			} -		} +		static $utc; -		// Zone offset -		$zone_offset = $this->timezone + $this->dst; - -		// 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($this, "@$gmepoch", $utc); +		$time->setTimezone($this->timezone); -			if ($gmepoch > $midnight + 86400) -			{ -				$day = 'TOMORROW'; -			} -			else if ($gmepoch > $midnight) -			{ -				$day = 'TODAY'; -			} -			else if ($gmepoch > $midnight - 86400) -			{ -				$day = 'YESTERDAY'; -			} +		return $time->format($format, $forcedate); +	} -			if ($day !== false) -			{ -				return str_replace('||', $this->lang['datetime'][$day], strtr(@gmdate($date_cache[$format]['format_short'], $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->timezone; +		return new phpbb_datetime($this, $time, $timezone); +	} -		return strtr(@gmdate($date_cache[$format]['format_long'], $gmepoch + $zone_offset), $date_cache[$format]['lang']); +	/** +	* Get the UNIX timestamp for a datetime in the users timezone, so we can store it in the database. +	* +	* @param	string			$format		Format of the entered date/time +	* @param	string			$time		Date/time with the timezone applied +	* @param	DateTimeZone	$timezone	Timezone of the date/time, falls back to timezone of current user +	* @return	int			Returns the unix timestamp +	*/ +	public function get_timestamp_from_format($format, $time, DateTimeZone $timezone = null) +	{ +		$timezone = $timezone ?: $this->timezone; +		$date = DateTime::createFromFormat($format, $time, $timezone); +		return ($date !== false) ? $date->format('U') : false;  	}  	/** | 
