I have a datepicker in my site which formats the time in the current locale. So the time gets sent to the server in d/MM/yy-format.

The locale on the frontend is determined by Magento\Framework\Stdlib\DateTime\TimezoneInterface::getDateFormat().

Now how can I convert this date to a database-friendly datetime (YYYY-MM-DD) or at least to a timestamp of some sort?

So basically I'm looking for a clean generic approach on how to convert a localized date to a timestamp.

I've tried all kind of things but none seemed to work.

4 Answers 4


Found the answer to my own question:

 * @var \Magento\Framework\Locale\ResolverInterface
protected $localeResolver;

... some code ...

$newDate = date('Y-m-d', (new \IntlDateFormatter(

This work for me but directly using object manager is not a good way inject TimezoneInterface into your constructor:

 $date = "1991-12-19 00:00:00"
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $timezoneInterface = $objectManager->create('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
 $formattedDate = $timezoneInterface->date($date)->format('Y/m/d');

Magento 2 and 1 are all PHP based, you could use the native (quicker) PHP function to get this without having to add more into your __constructs or using ObjectManager (which can be heavy):

1) Get today's date formatted:

$todayDate = date("Y-m-d H:i:s");

2) Get a specific date formatted:

$MyDate = date_create("2020-04-08");

$MyDateFormatted = date_format($MyDate, "Y/m/d H:i:s");

I hope this helps.


Make sure the date is converted to internal format. For example created_at date need to converted from MM/DD/YYYY to YYYY-MM-DD. So we have to use $this->_dateFilter object of \Magento\Framework\Stdlib\DateTime\Filter\Date Class and then use Zend_Filter_Input() function to convert to internal format. If $data['created_at'] = '07/27/2021'; after conversion to internal format created_at date will be $data['created_at'] = '2021-07-27';


namespace Vendor\Module\Controller\Adminhtml\MyController;

class Save extends \Magento\Backend\App\Action {

     * Date filter instance
     * @var \Magento\Framework\Stdlib\DateTime\Filter\Date
    protected $_dateFilter;

     * @param \Magento\Backend\App\Action\Context $context
     * @param \Vendor\Module\Model\Repository\MyModelRepository $myModel
     * @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
    public function __construct(
    \Magento\Backend\App\Action\Context $context,
    \Vendor\Module\Model\Repository\MyModelRepository $myModel, 
    \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
    ) {
        $this->_myModel = $myModel;
        $this->_dateFilter = $dateFilter;

    public function execute() {
        $data = $this->getRequest()->getParams();

        // make sure the date is converted to internal format
        $filterValues = ['created_at' => $this->_dateFilter];

        $inputFilter = new \Zend_Filter_Input($filterValues, [], $data);

        $data = $inputFilter->getUnescaped();

        $model = $this->_myModel();


        try {
        } catch (\RuntimeException $e) {
           echo  $e->getMessage();
        return $resultRedirect->setPath('*/*/');


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.