• I am using echo $dateTime->format(‘g:i A T’) to get time hour:minutes meridian Abbreviated timezone 7:30 PM IST.

    But this is giving me 5:30 PM +0530

    What am I missing? Please guide.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Did you specify the timezone first?

    $dateTime = new DateTime('now', new DateTimeZone('Asia/Calcutta'));

    echo $dateTime->format('g:i A T');

    Thread Starter sanumolu5

    (@sanumolu5)

    I did.

    The data value for T in a date-time format in PHP IS:

    Timezone abbreviation, if known; otherwise the GMT offset.

    It looks like the timezone abbreviation is known, so it shows that and not the offset time.

    If you want to show the offset (like +530 in your example), try using O. If that isn’t what you want, look at the list at https://www.php.net/manual/en/datetime.format.php and see what does what your after.

    Thread Starter sanumolu5

    (@sanumolu5)

    @catacaustic ,

    Thank you for your reply. I want to show timezone as “IST”, “CST” etc.. in PHP code. But looks like using PHP Datetime -> format(T”), it isn’t showing abbreviated timezone, rather it is showing the offset format. And this is happening for almost all the countries across the world, except for places in “US&Canada” timezone.

    Moderator bcworkz

    (@bcworkz)

    eriksandall’s suggestion yields 1:46 PM IST when I tried it just now. Have you tried the more modern DateTimeZone('Asia/Kolkata') instead? Either works for me.

    If your output is +0530 instead, it means the PECL timezone data is incomplete on your server, so PHP has no way to translate DateTimeZone('Asia/Kolkata') into IST.

    The latest PECL timezone data is available here. If your issue is on a hosted server you likely need your host to help you with this.

    Thread Starter sanumolu5

    (@sanumolu5)

    Hi @bcworkz ,

    Thank you for your reply. I contacted Siteground support (Our hosting server is Siteground) and they said there is nothing wrong at their end. It seems, since we are currently using PHP 7.4, the version of the timezone database is 2022.01.

    This means that the list of countries which is available for us was last updated on this date, and that all countries should be available there.

    Below is my PHP code:

    //Using cookie
        global $user_timezone_offset;
        $userTimezone;
    
        $offset_seconds = -$user_timezone_offset*60; // The timezone offset to search for in seconds
        $timezone_identifiers = DateTimeZone::listIdentifiers(); // Get a list of timezone identifiers
        foreach ($timezone_identifiers as $timezone_identifier) {
            $timezone = new DateTimeZone($timezone_identifier);
            $timezone_offset = $timezone->getOffset(new DateTime());     
            if ($timezone_offset == $offset_seconds) {              
                $userTimezone = new DateTimeZone($timezone_identifier);           
                break;
            }
        }
    
        $originalTimezone = new DateTimeZone('America/Chicago');    
        $dateTime = new DateTime($input_arr['input_time'], $originalTimezone);   
        $dateTime->setTimezone(new DateTimeZone($userTimezone->getName()));
    
        echo $dateTime->format('g:i A T');

    And it is showing offset as shown in the screenshot.

    https://www.awesomescreenshot.com/image/39326805?key=73f494d9ec20671e53134cfe4bdf35ec

    Moderator bcworkz

    (@bcworkz)

    I’m unable to test your code to see if there are any issues since it contains undefined vars. But as I’ve said, eriksandall’s suggestion does yield the desired output. However, my testing is with PHP 8.2, timezonedb 2022.05. 7.4 is no longer supported and I recommend you upgrade to at least 8.0. As you may be aware, doing so can cause issues in your WP installation. Such issues are invariably due to plugins or themes that have not been updated or are no longer supported. If you are reluctant to update, I cannot say I blame you, but sooner or later you will likely be forced to update.

    In any case, you’re strictly using native PHP methods, this has nothing to do with WordPress itself, it’s an issue with your PHP installation since no one else here can replicate the behavior you are seeing.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Unable to get timezone in abbreviated format in wordpress’ is closed to new replies.