Magento – Converting a locale date to YYYY-MM-DD

datelocalisationmagento-2.1magento2

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.

Best Answer

Found the answer to my own question:

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

... some code ...

$newDate = date('Y-m-d', (new \IntlDateFormatter(
    $this->localeResolver->getLocale(),
    \IntlDateFormatter::SHORT,
    \IntlDateFormatter::NONE
))->parse($localizeDate));
Related Topic