the_date() vs. the_time()

Nick

These two WordPress functions can be somewhat confusing, and and for good reason: they are nearly identical, and used in similar locations. Both are used to display the publish date (and time) of the post. How are they different?

the_date()

the_date() will only display the same date once per page. This is designed to be used primarily in post lists where you want to group posts by publish date, and it would be redundant to show the same date twice.

For example,

<?php 
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();

the_date('Y-m-d', '<h5>', '</h5>'); 
the_title();

endwhile;
endif;
?>

Might produce a list of posts like this:

2017-4-12

Test Post

Another Post

2017-4-11

A third post

A final post

the_time()

the_time() is more commonly used to get nearly the same results. It will display the date (and time) every time it used, whether on an archive or post template. Similar to the_date(), it must be used within The Loop.

If you’re in doubt, use the_time()

Example usage:

<div><?php the_time('Y-m-d'); ?></div>

Formatting

Both functions use standard PHP date/time formatting. However, in a WordPress context, it’s a good idea to use the format that has been set in the admin interface (that’s the point of a CMS, right??). For example:

<?php the_time( get_option( 'date_format' ) ); ?>