How to get all time zones lists as array in Magento 2

…衆ロ難τιáo~ 提交于 2019-12-07 14:33:41

问题


I have to add one form field in custom admin module that enables the users to select the timezone. So how to get the timezone lists?

This code snippet gives timezones list in Magento 1. But how to get same thing in Magento 2?

$timezones = Mage::getModel('core/locale')->getOptionTimezones();

回答1:


You should use the \Magento\Config\Model\Config\Source\Locale\Timezone::toOptionArray() like this:

/**
 * @param \Magento\Config\Model\Config\Source\Locale\Timezone $timezone
 */
public function __construct(
    \Magento\Config\Model\Config\Source\Locale\Timezone $timezone
) {
    $this->timezone = $timezone;
}

then in your code:

$options = $this->timezone->toOptionArray();


来源:https://stackoverflow.com/questions/41460317/how-to-get-all-time-zones-lists-as-array-in-magento-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!