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 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.

Let’s look at each of these, and how they work.

We’ll also show you a chart of how to write dates and times in PHP, so you can output those exactly as you intend.

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.

Character Description Example Output
Y Numeric 4-digit year Ex: 2012, 2015
y Numeric 2-digit year Ex: 01, 16
F Full text of the current month January—December
m Number of current month, with leading zeros 01—12
n Number of current month, no leading zeros 1—12
M Three letters of current month Jan—Dec
l Current day, full name (lowercase “L”) Sunday—Saturday
D Current day, three letters Mon—Sun
d Day of the month, with leading zeroes 01—31
j Day of the month, no leading zeroes 1—31
S English suffix for day of the week st, nd, rd, th (1st, 2nd, 3rd, 4th, etc.)
z Numeric day of the year (starts with 0) 0 through 365
H Hour, 24 hour, with leading zeroes 00 — 23
G Hour, 24 hour, no leading zeroes 0 — 23
h Hour, 12 hour, with leading zeroes 01 — 12
g Hour, 12 hour, no leading zeroes 1 — 12
i Minutes, with leading zeroes 00 — 59
s Seconds, with leading zeroes 00 — 59
a Time of day, lowercase am, pm
A Time of day, uppercase AM, PM
T Timezone abbreviation EST, PST, MDT, etc.
e Timezone identifier UTC, 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 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 *.