Blog: WordPress
Antique clock face

Dates and Times In WordPress

Avatar for John Locke

John Locke is a SEO consultant from Sacramento, CA. He helps manufacturing businesses rank higher through his web agency, Lockedown Design & SEO.

WordPress has built in functions that deal with dates and times. A few of these of these functions are the_date, the_time, and the_modified_time.

the_time and the_modified_time

The WordPress function, the_time returns the date a post or page was published. This function must be used in The Loop.

This function only takes one parameter, the format of the date.

The function, the_modified_time works in a similar fashion, returning the time the post was last modified.

Where the_modified_time and the_time are most crucial are on large content sites.

While websites in general are slowly moving towards including Schema.org metadata in their page information, large news and content sites that appear at the top of Google results are often time-stamped.

Whether or not you choose to add dates and time stamps to your web pages depends greatly on what purpose your site serves. But having the ability to automatically add them via a WordPress page template is handy.

Formatting Time Values In WordPress

WordPress uses PHP for it’s templates. The WordPress time and date functions use the same variables for time as PHP does, as we showed in the last article. Here’s that table again.

CharacterDescriptionExample Output
YNumeric 4-digit yearEx: 2012, 2015
yNumeric 2-digit yearEx: 01, 16
FFull text of the current monthJanuary—December
mNumber of current month, with leading zeros01—12
nNumber of current month, no leading zeros1—12
MThree letters of current monthJan—Dec
lCurrent day, full name (lowercase “L”)Sunday—Saturday
DCurrent day, three lettersMon—Sun
dDay of the month, with leading zeroes01—31
jDay of the month, no leading zeroes1—31
SEnglish suffix for day of the weekst, nd, rd, th (1st, 2nd, 3rd, 4th, etc.)
zNumeric day of the year (starts with 0)0 through 365
HHour, 24 hour, with leading zeroes00 — 23
GHour, 24 hour, no leading zeroes0 — 23
hHour, 12 hour, with leading zeroes01 — 12
gHour, 12 hour, no leading zeroes1 — 12
iMinutes, with leading zeroes00 — 59
sSeconds, with leading zeroes00 — 59
aTime of day, lowercaseam, pm
ATime of day, uppercaseAM, PM
TTimezone abbreviationEST, PST, MDT, etc.
eTimezone identifierUTC, GMT, etc.

If we go by this chart, we see that this function:


<?php the_time('F jS, Y'); ?>

will return the full month, the day (with suffix), and year. For example: October 1st, 2015.

the_date()

The WordPress function the_date() returns the date a page was originally published. It must be used within The Loop.

But unlike the_time, the_date takes more than one parameter.

Here’s the parameters that the_date accepts.


<?php the_date( $format, $before, $after, $echo ); ?>

Let’s look at what each parameter means.

$format

The $format is the date/time format. If no time format is defined, this will default to the time settings in your WordPress options.

You can use the_date to return just the date a post was published. This is the default use of the function.


<?php the_date('F jS, Y'); ?>

$before, $after

The $before and $after parameters will be outputted before and after the date/time format.

Here’s how you would output the date, wrapped in a paragraph tag, with some text before the date:


<?php the_date('F jS Y', '<p>Posted on ', '</p>'); ?>

$echo

The $echo parameter is a Boolean variable — it must be set to TRUE or FALSE.

This function defaults to TRUE, which means it prints the date to the screen. FALSE means it returns the date to be used in PHP.

Here’s an example where we store the date in a PHP variable, and then echo the variable to the screen. This accomplishes the same results as the example we used above, but there can be more complex uses for this in certain circumstances.


<?php $my_date = the_date('', '<p>Posted on ', '</p>', FALSE); echo $my_date; ?>

Using get_the_date

There are certain advantages to using get_the_date instead of the_date, especially on archive pages.

You see, the default behavior of the_date on an archive page is to only display the publish date for the first post for each day. This would also be true for instances like a RSS newsletter feed.

In these cases, use get_the_date instead.


<span class="entry-date"><?php echo get_the_date(); ?></span>

Unlike the_date, the get_the_date template tag always displays the date the current post was written.


You can learn more about functions that deal with dates and times in WordPress in the WordPress Codex.

Avatar for John Locke

John Locke is a SEO consultant from Sacramento, CA. He helps manufacturing businesses rank higher through his web agency, Lockedown Design & SEO.

2 comments on “Dates and Times In WordPress

  1. Hi John,

    Great introductory article for dates and times.

    Somehow I always use the function get_the_date() and echo it out, since the function the_date() will display only on the first post. If the following other posts are published on the same date, they won’t display that.

    I remember discovering that only after I spent some time figuring out why the date won’t show on archive pages. That was a few years ago. Single pages should not have any problems using that, though.

    Wanted to say that for others if they ever come to the same problem.

  2. Hi Igor:

    I appreciate you stopping by. You are absolutely correct — the_date() will not repeat on subsequent archived articles, if you have published more than one on a single day.

    Thanks for reminding me of that, I will add that to the article later today.

Join the Conversation

Your email address will be kept private. Required fields marked *.