the_date( string $format = '', string $before = '', string $after = '', bool $echo = true ): string|void
Displays or retrieves the date the current post was written (once per date)
Description
Will only output the date if the current post’s date is different from the previous one output.
i.e. Only one date listing will show per day worth of posts shown in the loop, even if the function is called several times for each post.
HTML output can be filtered with ‘the_date’.
Date string output can be filtered with ‘get_the_date’.
Parameters
-
$format
string Optional -
PHP date format. Defaults to the
'date_format'
option.Default:
''
-
$before
string Optional -
Output before the date.
Default:
''
-
$after
string Optional -
Output after the date.
Default:
''
-
$echo
bool Optional -
Whether to echo the date or return it.
Default:
true
Return
string|void String if retrieving.
Source
File: wp-includes/general-template.php
.
View all references
function the_date( $format = '', $before = '', $after = '', $echo = true ) {
global $currentday, $previousday;
$the_date = '';
if ( is_new_day() ) {
$the_date = $before . get_the_date( $format ) . $after;
$previousday = $currentday;
}
/**
* Filters the date a post was published for display.
*
* @since 0.71
*
* @param string $the_date The formatted date string.
* @param string $format PHP date format.
* @param string $before HTML output before the date.
* @param string $after HTML output after the date.
*/
$the_date = apply_filters( 'the_date', $the_date, $format, $before, $after );
if ( $echo ) {
echo $the_date;
} else {
return $the_date;
}
}
Hooks
-
apply_filters( 'the_date',
string $the_date ,string $format ,string $before ,string $after ) -
Filters the date a post was published for display.
Changelog
Version | Description |
---|---|
0.71 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Date as Year, Month, Date in Heading
Displays the date using the ‘2007-07-23’ format (ex: 2004-11-30), inside an <h2> tag.
Default Usage
Displays the date using defaults.
Date in Heading Using $my_date Variable
Returns the date in the default format inside an <h2> tag and assigns it to the
$my_date
variable. The variable’s value is then displayed with the PHP echo command.As stated in the documentation if there are multiple posts from the same day, it will only show once.
To show the date for posts published under same day, we should use the template tag the_time() or get_the_date() with a date-specific format string.
Both functions takes an optional parameter which follows the PHP Date Format
//add a specific date format with your loop
//example format: November 10, 2021. here You can see date Format String Examples::