How to display Date and Time on WordPress

Unless you’re a casual Javascript (client-side) or PHP (server-side) user, creating or finding an easy code to show the current date and time on your WordPress website can be a task.

But it’s not; it’s actually very simple to create your own shortcode using PHP and your theme’s functions.php file to display the date and time on any page or post found on your WordPress website.

1. Copy and paste the snippet below into your functions.php file to create [date] shortcode

function displaydate(){
return date('F jS, Y');
}
add_shortcode('date', 'displaydate');

2. Insert [date] where you would like to display the current date and time on any WordPress page or post.

To change the shortcode term “date” replace ‘date’ in the code above (‘SHORTCODE’, ‘displaydate’).

Displaying date and time can be done by using different PHP time constants

j = Day of the month without the leading 0 for single numbers (1 and 31)

d = Day of the month with with leading zeros for single numbers (01 and 31)

S = English ordinal suffix for the day of the month, in 2 characters

F = A full textual representation of a month, (January)

Y = A full numeric representation of a year, in 4 digits (2012)

In the PHP code we created above we use J and s together, such as jS, which is commonly used. It’s also easy to create your own timestamps or date and time signatures using PHP constants. Replace the constants F, jS, and Y in the code below. To find more visit the PHP date manual.

Sean Doyle

Sean is a distinguished tech author and entrepreneur with over 20 years of extensive experience in cybersecurity, privacy, malware, Google Analytics, online marketing, and various other tech domains. His expertise and contributions to the industry have been recognized in numerous esteemed publications. Sean is widely acclaimed for his sharp intellect and innovative insights, solidifying his reputation as a leading figure in the tech community. His work not only advances the field but also helps businesses and individuals navigate the complexities of the digital world.

11 Responses

  1. Jenaro says:

    Hi guys!
    My web is in spanish and the date used with the shortcode is in english. Do you know how I can change that?
    Best regards.

  2. D'Jason Rocker says:

    Hello, this is quite interesting.
    Could you please change this PHP code in to shortcode for me. I am very new to wordpress and I don’t know how to do it.

    Thanks

  3. chris says:

    Hi. The article title is how to display the date and time. Does this code display the date only? I just want the local time to appear at the very top of my WP site, near the top most menu. Site is not live yet, just working on it using MAMP.
    Thank you so much for any help.

  4. Jon says:

    Thanks for the info.

    Worked like a charm.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.