How to display Date and Time on WordPress

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.