• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

Date Format in WordPress

Admin

Well-Known Member
Staff member
Administrator
Two common WordPress tag functions you will see in most themes are the time and date format tags:
<?php the_date(); ?>
and
<?php the_time() ?>
. 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.
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:
<?php the_time('m.d.Y'); ?>
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.
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:
<?php the_time('d.m.Y'); ?>
What if you want to display the time and date in a different format altogether? Here are some options:
FORMAT CHARACTERS FOR THE ‘DAY’
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 ‘WEEK’
W = number representing the number of the week: ’32′ week of year
FORMAT CHARACTERS FOR THE ‘MONTH’
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 ‘YEAR’
Y = four digit year: 2009
y = two digit year: 09
FORMAT CHARACTERS FOR THE ‘TIME’
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
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:
<?php the_time('l'); ?> the <?php the_time('dS'); ?> of <?php the_time('F'); ?>
Will result in: Tuesday the 21st of April
<?php the_time('F dS, Y'); ?>
Will result in: April 21st, 2009
The possibilities are endless, so experiment and have fun!
 

Facebook Comments

New posts New threads New resources

Back
Top