如何在 Magento 2 中将所有时区列表作为数组
How to get all time zones lists as array in Magento 2
我必须在自定义管理模块中添加一个表单字段,使用户能够 select 时区。那么如何获取时区列表呢?
此代码片段给出了 Magento 1 中的时区列表。但是如何在 Magento 2 中获得相同的内容?
$timezones = Mage::getModel('core/locale')->getOptionTimezones();
您应该像这样使用 \Magento\Config\Model\Config\Source\Locale\Timezone::toOptionArray()
:
/**
* @param \Magento\Config\Model\Config\Source\Locale\Timezone $timezone
*/
public function __construct(
\Magento\Config\Model\Config\Source\Locale\Timezone $timezone
) {
$this->timezone = $timezone;
}
然后在您的代码中:
$options = $this->timezone->toOptionArray();
我必须在自定义管理模块中添加一个表单字段,使用户能够 select 时区。那么如何获取时区列表呢?
此代码片段给出了 Magento 1 中的时区列表。但是如何在 Magento 2 中获得相同的内容?
$timezones = Mage::getModel('core/locale')->getOptionTimezones();
您应该像这样使用 \Magento\Config\Model\Config\Source\Locale\Timezone::toOptionArray()
:
/**
* @param \Magento\Config\Model\Config\Source\Locale\Timezone $timezone
*/
public function __construct(
\Magento\Config\Model\Config\Source\Locale\Timezone $timezone
) {
$this->timezone = $timezone;
}
然后在您的代码中:
$options = $this->timezone->toOptionArray();