-2

I am using the below code to display current time in php supported time zones:

<?php
date_default_timezone_set('Asia/Kolkata');
$currentTime = date( 'd-m-Y h:i:s A', time () );
echo $currentTime;
?>

We need to create few different pages in wordpress to display current time in different supported cities / timezones in wordpress pages through shortcodes in following format:

[current_time timezone="Africa/Accra"]

Please help us to write this function.

1
  • And what solutions have you already tried? Could you show us your current code? Jan 16, 2019 at 7:48

2 Answers 2

2

Add this code in Theme function file functions.php

    function timeZone_funch( $atts ) 
    {
        extract(shortcode_atts(array('timezone'    => 'Asia/Kolkata'), $atts)); 
/* Asia/Kolkata is default Timezone */
        $output             = '';
        if (in_array($timezone, DateTimeZone::listIdentifiers()))
        {
            date_default_timezone_set($timezone);
            $currentTime = date( 'd-m-Y h:i:s A');
            $output = $currentTime;
        }
        else {
            $output = "Invalid Timezone";
        }

        return $output;
    }
    add_shortcode( 'current_time', 'timeZone_funch' );

USAGE: [current_time timezone="Africa/Accra"]

1
  • This is perfect. I am not sure why the question was down voted.
    – Skotlive
    Jan 25, 2019 at 11:31
0

/* Africa/Nairobi is default Timezone */ $output = ''; if (in_array($timezone, DateTimeZone::listIdentifiers())) { date_default_timezone_set($timezone); $currentTime = date( 'h:i:s A'); $output = $currentTime; } else { $output = "Invalid Timezone"; }

    return $output;
}
add_shortcode( 'current_time', 'timeZone_funch' );

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.