Convert Date According to Specific Locale Magento 2 : Today we will learn how to convert a date to a specific locale.
So magento has an interface DateTimeFormatterInterface, through which we can convert a date to any other locale according to our need.
check the below code.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$dateTimeFormatter = $objectManager->get('Magento\Framework\Stdlib\DateTime\DateTimeFormatterInterface');
$staticDate = new \DateTime('2012-07-31');
/**
* Returns a translated and localized date string
*
* @param \IntlCalendar|\DateTime $object
* @param string|int|array|null $format
* @param string|null $locale
* @return string
*/
$convertedDate = $dateTimeFormatter->formatObject($staticDate, \IntlDateFormatter::FULL, 'es_AR');
So convertedDate variable contains translated and localized date string.
Hope so it will help.
2 comments