current_time()

current_time()函数是Wordpress函数,根据指定的类型检索当前时间。

current_time( string $type, int|bool $gmt )

说明(Description)

“mysql”类型将以mysql DATETIME字段的格式返回时间。“timestamp”类型将返回当前时间戳或时间戳和时区偏移量的总和,具体取决于$gmt。其他字符串将被解释为PHP日期格式(例如“Y-m-d”)。

如果$gmt设置为“1”或“true”,则两种类型都将使用gmt时间。如果$gmt为false,则输出将使用WordPress选项中的gmt偏移量进行调整。


参数(Parameters)

参数类型必填说明
$type(string)必需要检索的时间类型。接受“mysql”、“timestamp”或PHP日期格式字符串(例如“Y-m-d”)。
$gmt(int | bool)可选是否使用GMT时区。默认为false。

返回(Return)

(int|string)如果$type是’timestamp’则为整数,否则为string。


源码(Source)

/**
 * Retrieve the current time based on specified type.
 *
 * The 'mysql' type will return the time in the format for MySQL DATETIME field.
 * The 'timestamp' type will return the current timestamp.
 * Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d').
 *
 * If $gmt is set to either '1' or 'true', then both types will use GMT time.
 * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.
 *
 * @since 1.0.0
 *
 * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date
 *                       format string (e.g. 'Y-m-d').
 * @param int|bool $gmt  Optional. Whether to use GMT timezone. Default false.
 * @return int|string Integer if $type is 'timestamp', string otherwise.
 */
function current_time( $type, $gmt = 0 ) {
	switch ( $type ) {
		case 'mysql':
			return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
		case 'timestamp':
			return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
		default:
			return ( $gmt ) ? date( $type ) : date( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
	}
}
更新版本源码位置使用被使用
1.0.0wp-includes/functions.php:71332

笔记(Notes)

日期/时间组件将在WordPress5.3中更新,人们应该注意以下几点:
检查结果
代码片段给出带有“split”函数的警告,因为

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索