WordPress at a glance

get_the_date() WP 3.0.0

Gets the creation date of the current post. you can specify an arbitrary date output format. Used in a WordPress loop.

This function is different from the_date(). It always return a result (date), even if the next post (in the loop) was published on the same day (the_date() will skip such date output).

You can use the get_the_date filter to change the output string.

Is the basis for: the_date()
Works based on: mysql2date()
✈ 1 time = 0.000723s = slow | 50000 times = 1.44s = fast PHP 7.2.5, WP 4.9.6
Hooks from function:
Return

false/String. Date the current post was written. False on failure.

Usage

get_the_date( $d, $post );
$d(string)
PHP date format defaults to the date_format option if not specified.
Default: ''
$post(int/WP_Post)
Post ID or WP_Post object.
Default: current post

Examples

#1 A basic example

Display the date of the post within the WordPress Loop.

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

#2 Specify an arbitrary date format

Post published at <?php echo get_the_date('n-j-Y'); ?>

Displays: Post published at 11-6-2018

Code of get_the_date: wp-includes/general-template.php VER 4.9.8

<?php
function get_the_date( $d = '', $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	if ( '' == $d ) {
		$the_date = mysql2date( get_option( 'date_format' ), $post->post_date );
	} else {
		$the_date = mysql2date( $d, $post->post_date );
	}

	/**
	 * Filters the date a post was published.
	 *
	 * @since 3.0.0
	 *
	 * @param string      $the_date The formatted date.
	 * @param string      $d        PHP date format. Defaults to 'date_format' option
	 *                              if not specified.
	 * @param int|WP_Post $post     The post object or ID.
	 */
	return apply_filters( 'get_the_date', $the_date, $d, $post );
}

Related Functions

From tag: date time

More from category: Posts, pages...

More from Template Tags: Posts, Pages, ...

No comments
    Hello, !     Log In . Register