Show Month Name, Year & Date in WordPress Post, Page, Widget

Simple instructions on how to show the name of current month, year or any date information on WordPress posts, pages and widgets; without a plugin.

Note! You will only need some functions.php editing skills.

Month Calendar

Not just Google but also blog visitors like to have the feeling that posts are up-to-date; and displaying the name of the current month, date will do just that.

Few basics:

The code below should be added to your functions.php file (usually the bottom); do that from WordPress dashboard: Appearance –> Editor –> Theme Functions.

This will create a shortcode “example: [monthname]” that you can place anywhere in a post or page; if you want to put that into a text or html widget — you will need to add the following code to functions.php first:

add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'widget_text', 'do_shortcode' );

The part “date(‘D, j F Y’)” defines the date format; you can edit it with many different parameters (see examples below) — you can find a full list here.

By default the server time is used — I’ve added examples of how to show the date from a different time zone — see list of supported time zones.

Few warnings:

Make sure that you are using a WordPress child theme or otherwise the functions.php will be overwritten by any theme updates.

If you make a mistake in functions.php; WordPress can stop working until you remove any broken code from functions.php.

Then let’s get to copy pasting:

This shortcode “[monthname]” will show the full name of the current month:

function monthname_function() {
$currentmonth = date('F');
return $currentmonth; }

add_shortcode('monthname', 'monthname_function');

Example: Today is the month of September — am I correct?

This shortcode “[yearnumber]” will show the number of the current year:

function yearnumber_function() {
$currentyear = date('Y');
return $currentyear; }

add_shortcode('yearnumber', 'yearnumber_function');

Example: The year is 2017 — and you’re not getting younger.

This shortcode “[fulldate]” will show the full date:

function fulldate_function() {
$currentdate = date('l, j F Y');
return $currentdate; }

add_shortcode('fulldate', 'fulldate_function');

Example: Today’s date is: Tuesday, 26 September 2017 — says my server.

This shortcode “[weekdaynametokyo]” will show the weekday in Tokyo:

function weekdaynametokyo_function() {
date_default_timezone_set('Asia/Tokyo');
$currentweekday = date('l');
return $currentweekday; }

add_shortcode('weekdaynametokyo', 'weekdaynametokyo_function');

This shortcode “[weekdaynameny]” will show the weekday in New York:

function weekdaynameny_function() {
date_default_timezone_set('America/New_York');
$currentweekday = date('l');
return $currentweekday; }

add_shortcode('weekdaynameny', 'weekdaynameny_function');

Example: It’s Tuesday in Tokyo; but Tuesday in New York.

And that’s all there is to it; no WordPress plugins needed.
Is the month name, year, or date info displaying correctly?
Comments, questions are welcome!

Twitter Button Facebook Button Google Button LinkedIn Button

Leave a Reply

Your email address will not be published. Required fields are marked *