Skip to content

Formatting Date and Time with Timezone Abbreviation

I have overridden the formatDateTime function of the MagentoFrameworkStdlibDateTimeTimezone 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.