3

I have overridden the formatDateTime function of the \Magento\Framework\Stdlib\DateTime\Timezone class in Magento 2 to display the timezone abbreviation along with the formatted date. However, I noticed that when using the 'z' pattern, it only returns the abbreviation without the full date.

I want to show date like Aug 9, 2023, 5:28:31 AM (EDT)

Here's the overridden method:

public function formatDateTime($date, $dateType = IntlDateFormatter::SHORT, $timeType = IntlDateFormatter::SHORT, $locale = null, $timezone = null, $pattern = null): string
{
    // Call the parent method to get the formatted date
    $formattedDate = parent::formatDateTime($date, $dateType, $timeType, $locale, $timezone, $pattern);
    
    // Get the timezone abbreviation
    $timezoneAbbreviation = parent::formatDateTime($date, $dateType, $timeType, $locale, $timezone, 'z');

    // Concatenate the timezone abbreviation to the formatted date
    $formattedDate .= ' (' . $timezoneAbbreviation . ')';

    return $formattedDate;
}

Is there any alternative method or pattern that can be used to obtain the full date along with the timezone abbreviation in a single call to the formatDateTime function? Any insights or suggestions would be greatly appreciated.

1 Answer 1

0

I will share the formatDate directly with the object manager.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$objDate = $objectManager->create('Magento\Framework\Stdlib\DateTime   \DateTime'); 
$date = $objDate->gmtDate(); 
$newDate = date("M,d-Y h:i:s A e", strtotime($date));

show the below link for the date formatDate you want to.

https://i.sstatic.net/rF5Bg.png

1
  • Thanks for you answer but i want it based on timezone selected for store and not in UTC it should be like EDT
    – Zahirabbas
    Commented Dec 15, 2023 at 6:05

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.