mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-15 22:09:48 +08:00
Merge pull request #2155 from dizews/timezone-in-base-formatter
add timeZone into base formatter
This commit is contained in:
@@ -104,6 +104,7 @@ Yii Framework 2 Change Log
|
||||
- Enh: Added support to parse json request data to Request::getRestParams() (cebe)
|
||||
- Enh: Added ability to get incoming headers (dizews)
|
||||
- Enh: Added `beforeRun()` and `afterRun()` to `yii\base\Action` (qiangxue)
|
||||
- Enh: Added support formatter timeZone by default (dizews)
|
||||
- Chg #1519: `yii\web\User::loginRequired()` now returns the `Response` object instead of exiting the application (qiangxue)
|
||||
- Chg #1586: `QueryBuilder::buildLikeCondition()` will now escape special characters and use percentage characters by default (qiangxue)
|
||||
- Chg #1610: `Html::activeCheckboxList()` and `Html::activeRadioList()` will submit an empty string if no checkbox/radio is selected (qiangxue)
|
||||
|
||||
@@ -27,6 +27,15 @@ use yii\helpers\Html;
|
||||
*/
|
||||
class Formatter extends Component
|
||||
{
|
||||
/**
|
||||
* @var string|\IntlTimeZone|\DateTimeZone the timezone to use for formatting time and date values.
|
||||
* This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php)
|
||||
* e.g. `UTC`, `Europe/Berlin` or `America/Chicago`.
|
||||
* Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
|
||||
* This can also be an IntlTimeZone or a DateTimeZone object.
|
||||
* If not set, [[\yii\base\Application::timezone]] will be used.
|
||||
*/
|
||||
public $timeZone;
|
||||
/**
|
||||
* @var string the default format string to be used to format a date using PHP date() function.
|
||||
*/
|
||||
@@ -59,12 +68,20 @@ class Formatter extends Component
|
||||
*/
|
||||
public $thousandSeparator;
|
||||
|
||||
|
||||
/**
|
||||
* Initializes the component.
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
if ($this->timeZone === null) {
|
||||
$this->timeZone = Yii::$app->timeZone;
|
||||
}
|
||||
if (is_string($this->timeZone)) {
|
||||
$this->timeZone = new \DateTimeZone($this->timeZone);
|
||||
} elseif ($this->timeZone instanceof IntlTimeZone) {
|
||||
$this->timeZone = $this->timeZone->toDateTimeZone();
|
||||
}
|
||||
|
||||
if (empty($this->booleanFormat)) {
|
||||
$this->booleanFormat = [Yii::t('yii', 'No'), Yii::t('yii', 'Yes')];
|
||||
}
|
||||
@@ -258,7 +275,7 @@ class Formatter extends Component
|
||||
return $this->nullDisplay;
|
||||
}
|
||||
$value = $this->normalizeDatetimeValue($value);
|
||||
return date($format === null ? $this->dateFormat : $format, $value);
|
||||
return $this->formatTimestamp($value, $format === null ? $this->dateFormat : $format, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,7 +299,7 @@ class Formatter extends Component
|
||||
return $this->nullDisplay;
|
||||
}
|
||||
$value = $this->normalizeDatetimeValue($value);
|
||||
return date($format === null ? $this->timeFormat : $format, $value);
|
||||
return $this->formatTimestamp($value, $format === null ? $this->timeFormat : $format, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -306,7 +323,7 @@ class Formatter extends Component
|
||||
return $this->nullDisplay;
|
||||
}
|
||||
$value = $this->normalizeDatetimeValue($value);
|
||||
return date($format === null ? $this->datetimeFormat : $format, $value);
|
||||
return $this->formatTimestamp($value, $format === null ? $this->datetimeFormat : $format, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -325,6 +342,19 @@ class Formatter extends Component
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param integer $value normalized datetime value
|
||||
* @param string $format the format used to convert the value into a date string.
|
||||
* @return string the formatted result
|
||||
*/
|
||||
protected function formatTimestamp($value, $format)
|
||||
{
|
||||
$date = new DateTime();
|
||||
$date->setTimestamp($value);
|
||||
$date->setTimezone($this->timeZone);
|
||||
return $date->format($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the value as an integer.
|
||||
* @param mixed $value the value to be formatted
|
||||
|
||||
@@ -38,15 +38,6 @@ class Formatter extends \yii\base\Formatter
|
||||
* If not set, [[\yii\base\Application::language]] will be used.
|
||||
*/
|
||||
public $locale;
|
||||
/**
|
||||
* @var string|\IntlTimeZone|\DateTimeZone the timezone to use for formatting time and date values.
|
||||
* This can be any value that may be passed to [date_default_timezone_set()](http://www.php.net/manual/en/function.date-default-timezone-set.php)
|
||||
* e.g. `UTC`, `Europe/Berlin` or `America/Chicago`.
|
||||
* Refer to the [php manual](http://www.php.net/manual/en/timezones.php) for available timezones.
|
||||
* This can also be an IntlTimeZone or a DateTimeZone object.
|
||||
* If not set, [[\yii\base\Application::timezone]] will be used.
|
||||
*/
|
||||
public $timeZone;
|
||||
/**
|
||||
* @var string the default format string to be used to format a date.
|
||||
* This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.
|
||||
@@ -98,9 +89,6 @@ class Formatter extends \yii\base\Formatter
|
||||
if ($this->locale === null) {
|
||||
$this->locale = Yii::$app->language;
|
||||
}
|
||||
if ($this->timeZone === null) {
|
||||
$this->timeZone = Yii::$app->timeZone;
|
||||
}
|
||||
if ($this->decimalSeparator === null || $this->thousandSeparator === null) {
|
||||
$formatter = new NumberFormatter($this->locale, NumberFormatter::DECIMAL);
|
||||
if ($this->decimalSeparator === null) {
|
||||
|
||||
Reference in New Issue
Block a user