Insert the Current Date/Time in Posts or Pages

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].

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.

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.

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.

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.

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.

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

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.

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.

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.

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 https://wordpress.org/plugins/wp-chrono/

Wish your site was as fast as this?

Do It With WordPress is proudly hosted by WP Engine, the very best WordPress hosting that money can buy.

Aside from being blazing fast, it's very secure, has a staging area to test changes before making them live, automated daily backups, malware scanning, stellar support from WordPress experts... It's just everything that you would want from your host, and more.