WordPress函数:current_time 返回当前时间

追格官方小助手/ 2023年03月15日/ WordPress/ 浏览 978

WordPress 提供了专门获取当前时间的函数:current_time。


函数原型:


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


$gmt 表示是否返回 GMT 时间,默认 false。


$type 为 'mysql' 时,返回 MySQL DATETIME 。为 'U' 或 'timestamp' 反回时间戳。另外还支持 PHP date 函数的 format 。


函数源码:


function current_time( $type, $gmt = 0 ) {
	// Don't use non-GMT timestamp, unless you know the difference and really need to.
	if ( 'timestamp' === $type || 'U' === $type ) {
		return $gmt ? time() : time() + (int) ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
	}

	if ( 'mysql' === $type ) {
		$type = 'Y-m-d H:i:s';
	}

	$timezone = $gmt ? new DateTimeZone( 'UTC' ) : wp_timezone();
	$datetime = new DateTime( 'now', $timezone );

	return $datetime->format( $type );
}


发表评论

暂无评论,抢个沙发...

客服 工单