How To Display Today’s Day or Date Within A WordPress Post

This article will quickly show you how to display today’s day or date within a WordPress post – in a variety of formats.

For example, if you wanted to display “Happy Sunday!” within the body/text of a post. The day or date will be dynamically generated by WordPress so you won’t have to update it manually every day!

I assumed it would be easy to find out how to do this on the web but it wasn’t. Every supposed ‘solution’ I found was about inserting today’s date in the Header (globally, across the site) or in a sidebar widget or about how to add/change the meta date ‘Last Updated On…’ at the top of every post.

I never found a solution to display today’s day or date within a WordPress post so I worked it out for myself and have written the simple guide below to show you how to do it.

Display Today's Day or Date Within A WordPress Post

Create a New Shortcode

First you need to add a snippet of code to your Functions.php file – this will create a new shortcode that you can just type into a post to display today’s day or date.

Tip: it is best to insert the snippet into Functions.php of your child theme so it is permanent. If you insert it into Functions.php of your main WordPress theme (e.g. 2020 or Genesis), it will only work until the next theme update, then you will have to insert it all over again…

For safety, take a backup of the file first so you can restore it if necessary…

Now copy and paste the following code into your Functions.php file then save it:

// Add todays date shortcode
function displayDateToday( $atts )
{
$date = date('l');
return $date;
}
add_shortcode( 'autotoday', 'displayDateToday');

This snippet adds a new shortcode of [autotoday] which we can use to display an automated representation of the day of the week e.g. Sunday.

However, if you want to display an alternative format of day (e.g. Sun), or a full date instead (e.g. Sunday April 19, 2020) you can easily change this behavior – see section 4 at the bottom of this page.

Use the New Shortcode in a Post

To use the new shortcode, simply type [autotoday] anywhere in a post and, after you publish or update it, the current day will be inserted dynamically. Note that you must include the square brackets – they tell WordPress it’s a shortcode, not normal text.

For example, if you had typed in the post: Hello everyone, Happy [autotoday]!

Then visitors to that post on a Sunday (*) would see: Hello everyone, Happy Sunday!

* Note: if you’re wondering why I’m not using the shortcode here myself, to dynamically display the current day – if I did, you wouldn’t be able to see the [autotoday] text in the example above because it would display as the current day instead…

Make Sure the Correct Day or Date is Cached!

If you do not use a caching plugin (e.g. W3 Total Cache) and do not have a cache enabled at server level (e.g. Cloudflare) then skip this section.

If you do use a form of caching then your post may be cached and served to visitors as a static page. This means that the day/date within the post will also be static – it will not change dynamically each day so will be incorrect until the cache is next cleared…

Here are three ways to avoid this issue, in order of preference:

  1. Best – set your caching system to re-cache the post every day, just after midnight. This will ensure that the cached post always displays the correct day or date.
  2. Alternative – set your caching system to re-cache the whole website every day, just after midnight.
    This works but is more of a server overhead – not much of an issue unless you have a very large site.
  3. Worst – set your caching system to never cache the post. This is not ideal as the post may load more slowly and/or the server will be put under more load.

(Optional) Alternative Formats of Day or Date

The part of the code snippet that controls the format of the day or date is this parameter:

date('l');

Within this parameter, the l (lowercase L) is a character that equals “a full textual representation of the day of the week i.e. Sunday through Saturday“.

But you can change this format by using different characters. All the characters that you can choose from are shown under the Parameters heading in this manual – combine different characters or reorder them to achieve the desired format.

For example, if you wanted to change the format to a date such as April 19 then change the parameter above to:

date('F j');

Or, if you wanted to change the format to a full date such as Sunday April 19, 2020 then change the parameter above to:

date('l F j, Y');

Character Rules:

Leave a gap between each character for a space:
e.g. Fj displays as April19
whereas F j displays as April 19

Add a comma after a character if you want one:
e.g. F j Y displays as April 19 2020
whereas F j, Y displays as April 19, 2020