Display Date And Time In WordPress

How To Display Date And Time In WordPress

In some cases, it’s important to display the Date And Time on your WordPress website. We can easily display date and time on our WordPress website using multiple methods. In this post, We are going to discuss several methods of how to display date and time on WordPress website.

Display Date And Time In WordPress Using a Shortcode :

It’s one of the easiest ways to display date and time in your WordPress website without using any plugin. We know plugins make our website slower. Always, we should try to use fewer plugins in our WordPress website to make it faster. So, using shortcode it’s the most convenient way to display date and time on your WordPress website without using any plugin.

Copy the below code and paste it at the bottom of your active theme’s functions.php file. We highly recommend using a child theme for any kind of custom code.

// ADD DATE AND TIME TO YOUR WORDPRESS WEBSITE

function current_time_date(){
return date('F jS, Y, H:i:s');
}
 
add_shortcode( 'time_date', 'current_time_date' );

After adding the above code to your functions.php file, now you need to add this shortcode [time_date] where you want to show the date and time on your WordPress website.

The result will look like the above screenshot marked in red color. You can add CSS & JS to make it more nicer. If you don’t like the above date and time format, you can change it easily by following the php manual from here.

NOTE: To use the shortcode to the Widgets on your website, you must use the below hook at the bottom of your active theme’s functions.php file.

add_filter('widget_text','do_shortcode');

Display Date And Time In WordPress Using Another Shortcode:

In some cases, if the above shortcode doesn’t work you can use the shortcode mentioned below to show the current date and time in your WordPress website.

// ADD DATE TO YOUR WORDPRESS WEBSITE

function fwp_date_today($atts, $content = null) {
extract( shortcode_atts( array(
        'format' => ''
    ), $atts ) ); 
 
if ($atts['format'] == '') {
$date_time .= date(get_option('date_format')); 
}  else { 
$date_time .= date($atts['format']); 
} 
return $date_time;
} 
add_shortcode('today-date','fwp_date_today');

To show the current date anywhere on your WordPress website, please use this shortcode [today-date]

NOTE: It will show the default date format from your website settings. You can change the format easily from your WordPress dashboard Settings » General page.

Display Current Date In WordPress Using A Single Line Of Code:

If you don’t like to edit your functions.php file, you can show the current date using a single line of code mentioned below.

<?php echo date(get_option('date_format')); ?>

NOTE: You need to use the above code in your active theme’s template where you want to show the current date in your WordPress site. It will display the current date according to your WordPress dashboard Settings » General page.

Display Date And Time Using A Plugin:

If you don’t have a piece of minimum knowledge of how to use custom code snippets, You can display the date and time using a plugin in your WordPress website. There are a lot of plugins in WP Plugin Directory to show the current date and time in your WordPress website but we recommend using this one.

Why it’s important to show the date and time on your website?

It’s not crucial to display the date and time on your website. But if you are an owner of a newspaper or blog website, you should show the date and time in a widget on your website. Besides, You may not have enough content to display in the header area of your website, in this case, you can display the date and time on your website.

N.B. This is a developer-level tutorial. If you feel, it’s difficult to do for you. You may consider hiring me through Fiverr. I’ll fix your issue as soon as possible.

Table of Contents :

Leave a Comment

Your email address will not be published.