get_the_time()获取当前文章/页面的发布时间

目录

描述

Returns the time of the current post for use in PHP. It does not display the time. To display the time of a post, use the_time(). This tag must be used within The Loop.

get_the_time() 描述

用法

 <?php get_the_time$format$post ); ?> 

get_the_time() 用法

参数

format

(string) (可选) The format used to display the time (and date). See Formatting Date and Time for other time and date formats. WordPress is written in the programming language PHP. The date formatting functions in WordPress use PHP's built-in date formatting functions.

默认值: Time Format configured in Administration > Settings > General

post

(integer/object) (可选) post ID or object.

默认值: null. global $post object

get_the_time() 参数

返回值

(string) 

Returns the time according to the default or specified time format.

get_the_time() 返回值

示例

Default Usage

Returns the time of the current post using the WordPress default format, and displays it using the PHP echo command.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php echo get_the_time(); ?>

Returns the time of the post with ID $post->ID in the WordPress default format.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php echo get_the_time('', $post->ID); ?>

Getting Unix Timestamp

Assigns the local time of the current post in seconds (since January 1 1970, known as the Unix Epoch) to the variable $u_time.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php $local_timestamp = get_the_time('U'); ?>

In most cases, you would probably want the epoch time for GMT (rather than for the local time zone), which you can get with the get_post_time() function, setting the $gmt option to true:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php $gmt_timestamp = get_post_time('U', true); ?>

get_the_time() 示例

源文件

get_the_time() 函数的代码位于 wp-includes/general-template.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve the time at which the post was written.
 *
 * @since 1.5.0
 *
 * @param string      $d    Optional. Format to use for retrieving the time the post
 *                          was written. Either 'G', 'U', or php date format defaults
 *                          to the value specified in the time_format option. Default empty.
 * @param int|WP_Post $post WP_Post object or ID. Default is global $post object.
 * @return false|string Formatted date string or Unix timestamp. False on failure.
 */
function get_the_time( $d = '', $post = null ) {
	$post = get_post($post);

	if ( ! $post ) {
		return false;
	}

	if ( '' == $d )
		$the_time = get_post_time(get_option('time_format'), false, $post, true);
	else
		$the_time = get_post_time($d, false, $post, true);

	/**
	 * Filter the time a post was written.
	 *
	 * @since 1.5.0
	 *
	 * @param string      $the_time The formatted time.
	 * @param string      $d        Format to use for retrieving the time the post was written.
	 *                              Accepts 'G', 'U', or php date format value specified
	 *                              in 'time_format' option. Default empty.
	 * @param int|WP_Post $post     WP_Post object or ID.
	 */
	return apply_filters( 'get_the_time', $the_time, $d, $post );
}

get_the_time() 源文件

相关