WordPress has built in functions that deal with dates and times. A few of these of these functions are
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_modified_time works in a similar fashion, returning the time the post was last modified.
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.
|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 WordPress function
the_date() returns the date a page was originally published. It must be used within The Loop.
the_time, the_date takes more than one parameter.
Here’s the parameters that
<?php the_date( $format, $before, $after, $echo ); ?>
Let’s look at what each parameter means.
$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'); ?>
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 parameter is a Boolean variable — it must be set to
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; ?>
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
<span class="entry-date"><?php echo get_the_date(); ?></span>
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.