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 Doyle is an engineer from Los Angeles, California. Sean's primary focuses include Internet Security, Web Spam, and Online Marketing.

Comments ( 11 )

  1. ReplyJenaro
    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. ReplyD'Jason Rocker
    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
    • ReplyD'Jason Rocker
      Here is the PHP code
      • ReplyD'Jason Rocker
        Here is the PHP code
    • ReplyD'Jason Rocker
      Here is the PHP code php echo human_time_diff( get_the_time('U',null), current_time('timestamp') ) . ' ago';
  3. Replychris
    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.
    • ReplyAuthorSean Doyle
      This displays the date and time only. To display the time on your website you would like need to use javascript and HTML. Here's a generator: Select 'no date' and select the time format you want to use to make it only show a clock. Hope it helps and good luck!
  4. ReplyJon
    Thanks for the info. Worked like a charm.