I'm getting very confused here. I made a module which has an adminhtml form which contains a date field. I'm adding it like this:
$quotaFieldset->addField(
'quota_expiry',
'date',
[
'name' => 'quota_expiry',
'label' => __('Quota Expiry Date'),
'title' => __('Quota Expiry Date'),
'required' => false,
'date_format' => $this->_localeDate->getDateFormat(),
'input_format' => \Magento\Framework\Stdlib\DateTime::DATE_INTERNAL_FORMAT,
'time' => false,
'disabled' => false
]
);
So far this has worked well. But now I have deployed my application to another server, and suddenly (I know this from debug logs) the string returned from $this->_localeDate->getDateFormat()
is now "dd/MM/y"
instead of "M/d/yy"
like on my development machine.
Because of that, when I use the popup calendar to select a date, it would fill in "29/04/2016" which then gives following exception when I save the model:
DateTime::__construct(): Failed to parse time string (29/04/2016) at position 0 (2): Unexpected character
#0 .../lib/internal/Magento/Framework/Stdlib/DateTime.php(55): DateTime->__construct('29/04/2016')
#1 .../lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php(2671): Magento\Framework\Stdlib\DateTime->formatDate('29/04/2016', true)
#2 .../lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php(2896): Magento\Framework\DB\Adapter\Pdo\Mysql->formatDate('29/04/2016')
#3 .../lib/internal/Magento/Framework/Model/ResourceModel/AbstractResource.php(184): Magento\Framework\DB\Adapter\Pdo\Mysql->prepareColumnValue(Array, '29/04/2016')
....
Where do I need to fix this? The locale settings in the Magento config are the same for all my store views (but then again, adminhtml is a different kind of store view and cannot be assigned a locale to my knowledge...?).
Or would this be lower, like the OS locale? But my environment is running in Docker containers and therefore it should be exactly the same, regardless if they run on my local or on the server.
I don't know what to do. Please help.
Edit: I have made some progress. I discovered that the admin store view locale can be set for every admin user in their settings, and one of mine was set to en_US while the other was en_GB.
But the question remains: how do I handle this correctly? I don't want my code to break when the admin user changes their locale.