Two common WordPress tag functions you will see in most themes are the time and date format tags:
Each character in these format strings represents one part of the object we are formatting. Let’s look at a simple example. Here is the date tag from the Slate theme:
In this instance, we are using the format string 'm.d.Y' to display the date in US format using all numbers. The character, lowercase m, formats the month displayed as a two digit number. The character, lowercase d, formats the day of the month displayed as a two digit number. The character, capital Y, formats the year as a four digit number.
In the Slate theme, I wanted periods to separate the parts of the date. You might prefer spaces, in which case, remove the periods and leave a space between the characters. Your date format string 'm d Y' will display with spaces, 'm-d-Y' with hyphens… you get the picture.
If you want to display the date in European format, follow this sequence. With a simple rearranging of the format string, we end up with this code:
FORMAT CHARACTERS FOR THE ‘DAY’
The possibilities are endless, so experiment and have fun!
and<?php the_date(); ?>
. These tags are used to return time and date information relating to the publishing of a post. Both of these functions accept a parameter that formats the way the date or time is displayed. This parameter is called a format string and can be thought of as a simple template describing how and what will appear. We are going to look at some of the options you have when formatting these functions.<?php the_time() ?>
Each character in these format strings represents one part of the object we are formatting. Let’s look at a simple example. Here is the date tag from the Slate theme:
You will notice that I used the the_time tag. This is because using the the_date tag within the WordPress loop will fail to return two instances of the same date on the same page. As a general rule use the_time when possible.<?php the_time('m.d.Y'); ?>
In this instance, we are using the format string 'm.d.Y' to display the date in US format using all numbers. The character, lowercase m, formats the month displayed as a two digit number. The character, lowercase d, formats the day of the month displayed as a two digit number. The character, capital Y, formats the year as a four digit number.
In the Slate theme, I wanted periods to separate the parts of the date. You might prefer spaces, in which case, remove the periods and leave a space between the characters. Your date format string 'm d Y' will display with spaces, 'm-d-Y' with hyphens… you get the picture.
If you want to display the date in European format, follow this sequence. With a simple rearranging of the format string, we end up with this code:
What if you want to display the time and date in a different format altogether? Here are some options:<?php the_time('d.m.Y'); ?>
FORMAT CHARACTERS FOR THE ‘DAY’
FORMAT CHARACTERS FOR THE ‘WEEK’D = abbreviated day of the week: e.g. Mon, Tue, Sun, etc.
d = date displayed as two digits, and single digit dates are led by a zero
j = date displayed as one digit
l (lowercase L) = full day of the week: e.g. Monday, Tuesday
N = number representing the day of the week: e.g. Mon.=>1
S = suffix for numeric day of the month: e.g. st, nd, ect.
z = number representing the day of the year: 0-365
FORMAT CHARACTERS FOR THE ‘MONTH’W = number representing the number of the week: ’32′ week of year
FORMAT CHARACTERS FOR THE ‘YEAR’F = full month: e.g. May, June
m = two digit number representing month, single digit dates are led by a zero
M = abbreviated month: e.g. Dec, June, etc.
n = one digit month
FORMAT CHARACTERS FOR THE ‘TIME’Y = four digit year: 2009
y = two digit year: 09
This list is not exhaustive, but it includes most of the format characters you might use in a WordPress theme. There are literally thousands of ways you could use these tags. Here are a couple of examples:g = one digit, 12-hour hour format
G = one digit, 24-hour hour format
h = two digit, 12-hour hour format
H = two digit, 24-hour hour format
a = lowercase am or pm
A = uppercase AM or PM
i = two digit, minutes
s = two digit, seconds
Will result in: Tuesday the 21st of April<?php the_time('l'); ?> the <?php the_time('dS'); ?> of <?php the_time('F'); ?>
Will result in: April 21st, 2009<?php the_time('F dS, Y'); ?>
The possibilities are endless, so experiment and have fun!