admin管理员组

文章数量:1279186

I am using the below code to display current date in php using shortcodes for Indian time zones:

add_shortcode( 'current_date', 'mth_footer_date2' );
function mth_footer_date2() {
    ob_start();
    date_default_timezone_set("Asia/Kolkata");
    echo gmdate("jS F Y");
    return ob_get_clean();
}

We need to add [current_date] Shortcode to display current date. This code is displaying date but I think, this is not in Indian Timezone. Please provide me such codes to display time in Indian Timezone. I want Date in following format, Sunday, 24th October, 2021.

Also provide ways to show tomorrow date in same format.

I am using the below code to display current date in php using shortcodes for Indian time zones:

add_shortcode( 'current_date', 'mth_footer_date2' );
function mth_footer_date2() {
    ob_start();
    date_default_timezone_set("Asia/Kolkata");
    echo gmdate("jS F Y");
    return ob_get_clean();
}

We need to add [current_date] Shortcode to display current date. This code is displaying date but I think, this is not in Indian Timezone. Please provide me such codes to display time in Indian Timezone. I want Date in following format, Sunday, 24th October, 2021.

Also provide ways to show tomorrow date in same format.

Share Improve this question edited Oct 24, 2021 at 14:20 Buttered_Toast 2,8191 gold badge8 silver badges21 bronze badges asked Oct 24, 2021 at 13:04 Amresh MishraAmresh Mishra 32 bronze badges 1
  • Instead of using the ob_start() / echo / ob_get_clean(), you can simply return date( "jS F Y" );. Note that, per the docs, shortcode callbacks should never output content directly, but should always return the content. – Pat J Commented Oct 24, 2021 at 15:48
Add a comment  | 

1 Answer 1

Reset to default 0

You need to use date() in place of gmdate(), which returns the date/time for GMT.

To get the output you want, change the code to:

echo date('l, jS F, Y');

For displaying tomorrow's date, use the following code accordingly:

$timestamp = strtotime('tomorrow');
echo date('l, jS F, Y', $timestamp);

本文标签: Display Current Date using shortcode