Insert the Current Date/Time in Posts or Pages

Clocks at Canary Wharf

Sometimes, you may want to display the current date or time inside one of your posts and pages and have it update dynamically every time the page is loaded.

We can achieve this quite simply by creating an easy shortcode in your WordPress installation.

To do this, add the following code to the bottom of your functions.php file. This code includes a function which checks the current date/time and then returns it when the shortcode is inserted in a post or page. You can change the way the date/time appears by using PHP date formats; the way I’ve written the code in this tutorial will return a value such as May 18, 2011.

Now all you need to do to display the date is write your shortcode into one of your posts or pages; in this case, the shortcode is [date].

25 thoughts on “Insert the Current Date/Time in Posts or Pages”

  1. Lisa says:

    Hmmm, what am I doing wrong? my page still just shows [date]

    Theme Functions
    Paste your code

    On page


    Nothing. Still shows [date]

    Can you help? I really appreciate the info on this site!

    1. It must be the way the code has been pasted into your functions.php file. Are your PHP tags open in the file? i.e. is there a bit at the end of the file that has ?> ? If so, you need to remove that in order for it to work.

  2. Mike V says:

    I am trying to do this too. Will this code in functions.php get overwritten every time I update WordPress? Is there a way to stop that?

    1. If you put it in your theme’s functions.php, it will get overwritten every time you update your theme (not WordPress). It was also be lost if you ever switch themes (since functions.php is theme dependent). If you haven’t already, I suggest having a look at including it in a functionality plugin.

  3. Ravi says:

    Hey Dave,

    Thanks for the info.

    The code I’m using is similar:

    // [date]
    function displaydate(){
    return date(‘l, F jS, Y’);
    add_shortcode(‘date’, ‘displaydate’);
    // end date

    What I would like to do now is add +1 day to today’s date.

    For example, the text would read ‘….offer runs out on [todays date +1 day].’

    Today it’s 15th July, so the text would then read ‘…offer runs out on Monday, 16ht July, 2012.’

    How do I create this shortcode? I’ve literally searched for hours on google but can’t find anything. I’ve found stuff on php but don’t know how to transfer that over to wordpress.

    Your help would be appreciated.

    – Ravi.

    1. You can do this by changing the date function to:

      date(‘l, F jS, Y’, strtotime("tomorrow"));

      Let me know if that does it.

  4. Margie says:

    I apologize if this is way beyond the scope for this post, but do you have any thoughts on how one might make a shortcode that would display a static date in which my client could use a date picker to select the date? I need the date to be something reliable because the shortcode will be generating a list of items from the database that fall between two dates.

    1. Well, a shortcode won’t do you any good, because it returns contents based on the syntax you enter, which is raw. The only way you could add the date picked is to add a button to the WYSIWYG editor that brings up a dialogue from which you can select the dates to enter into the shortcode, to then return the appropriate content from the database.

  5. George Trask says:

    This is a great help. Thanks. Dave. Here’s one more wrinkle:

    Could you please suggest a way to return a date that is X number of days in the future, for example, 14 days from today.


    1. Yes, you can use mktime to get the date in 14 days’ time and use then parse that to the date function as follows:

      $fourteendays = mktime(0, 0, 0, date("m") , date("d")+14, date("Y"));
      return date('F jS, Y', $fourteendays);

  6. Jason says:

    Hi, thanks for this. Got it working on my site immediately.

    However I’m trying to get something where it picks up the published date for that particular post. Any ideas?

    1. You could use something like this, and revert to the original get_the_time function, instead of get_the_modified_time, which shows when a post was modified rather than published.

  7. Rory says:

    Tried and get his:
    Fatal error: Call to undefined function add_shortcode() in /..path../wp-includes/functions.php on line (line of the added code).

    Any clues?

    1. You’re putting it in the wrong functions.php. Whenever someone references functions.php, they are talking about the functions.php file in your active theme, not in wp-includes, which should never be modified.

  8. stefan says:

    Hello Dave, tell me please, how can I make date to appear in Romanian language? WordPress and theme are translated into Romanian but date appears to me in English. I use your code [data] from the file functions.php


    1. Hi Stefan,

      The main difference is that you need to set the locale for time first and then use strftime() instead of date() to return the date/time, so you can update your shortcode function to look more like this to get the desired result. Let me know if that does the trick.

  9. Athen Chimenti says:

    Hi Dave, how do you make the date show a specific time zone? It works great but appears to be be set to UTC.


    1. It uses the server time. To use the site’s local time, replace date() with current_time(). To use a different time zone altogether, see this thread.

  10. Jatin says:


    I want to create this custom shortcode in Core PHP without wordpress. How this possible .Please help us..

    1. It’s already in PHP. Just remove the last line which adds it as a WordPress shortcode and call displaydate() in PHP instead.

  11. Arul Patmose says:

    Hi, I want to dynamically show the Last Posted date and time on Header. Please help me with the code that I can get the last posted date and time from the WP database. Thanks.

  12. As in, the last time that any post was published?

  13. Arul Patmose says:

    Thanks for your reply. I am not talking about a particular post. I am doing a news website. It’s being updated frequently and I want to show the visitor that the last news was published a few minutes ago or the exact time somewhere in header. Hope you may understand now.

    1. Right, so that’s to say that you want the time that the most recent post was published. As such, you’d need to do a WP_Query to get the most recent post (‘posts_per_page’ => 1) and then display the time that it was published (using the_date()). You should probably cache this result in a transient which you should then clear when a new post is published.

  14. Milan says:

    Hi everyone, while ago I was playing with date/time shortcodes. At the end I created free plugin which provides users with set of nice shortcodes, easy to use. This can be helpful both to web site owners and to WordPress developers (code is object oriented and easy to read and understand). This is a link to a plugin repo

    I would appreciate feedback and suggestions for future features. :)

Leave a Reply