问题
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