函数文档

current_time()

💡 云策文档标注

概述

current_time() 函数用于根据指定类型检索当前时间,支持 MySQL 格式、时间戳或自定义 PHP 日期格式。其行为受 $gmt 参数影响,决定是否使用 GMT 时间。

关键要点

  • 参数 $type 接受 'mysql'、'timestamp'、'U' 或 PHP 日期格式字符串(如 'Y-m-d'),分别返回相应格式的时间。
  • 参数 $gmt 为布尔值,默认为 false;当为 true 时使用 GMT 时间,否则根据站点 GMT 偏移调整输出。
  • 返回值为整数(当 $type 为 'timestamp' 或 'U')或字符串(其他类型)。
  • 从 WordPress 5.3.0 开始,$type 为 'U' 时返回整数,之前版本返回字符串。
  • 函数内部处理:对于 'timestamp' 或 'U' 类型,根据 $gmt 计算时间戳;对于 'mysql' 类型,转换为 'Y-m-d H:i:s' 格式;其他字符串使用 DateTime 对象格式化。
  • 相关函数包括 wp_timezone() 和 get_option(),用于获取时区和选项值。
  • 用户贡献笔记提示:自 WordPress 5.3 起,不推荐使用 current_time('timestamp'),建议改用 time()、current_datetime() 等替代方案,以避免时间处理问题。

代码示例

// 示例代码展示不同参数组合的用法
echo "current_time( 'mysql' ) returns local site time: " . current_time( 'mysql' ) . "<br />";
echo "current_time( 'mysql', 1 ) returns GMT: " . current_time( 'mysql', 1 ) . "<br />";
echo "current_time( 'timestamp' ) returns local site time: " . date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) ) . "<br />";
echo "current_time( 'timestamp', 1 ) returns GMT: " . date( 'Y-m-d H:i:s', current_time( 'timestamp', 1 ) ) . "<br />";

注意事项

  • 自 WordPress 5.3 起,避免使用 current_time('timestamp') 获取时间戳,因为它可能返回非 Unix 时间戳,推荐使用 time() 或 current_datetime()->getTimestamp() 等替代方法。
  • 处理时间时,注意 PHP 版本兼容性,例如 split() 函数已弃用,应使用 preg_split() 替代。
  • 确保理解 $gmt 参数的影响,以正确获取本地或 GMT 时间。

📄 原文内容

Retrieves the current time based on specified type.

Description

  • The ‘mysql’ type will return the time in the format for MySQL DATETIME field.
  • The ‘timestamp’ or ‘U’ types will return the current timestamp or a sum of timestamp and timezone offset, depending on $gmt.
  • Other strings will be interpreted as PHP date formats (e.g. ‘Y-m-d’).

If $gmt is a truthy value then both types will use GMT time, otherwise the output is adjusted with the GMT offset for the site.

Parameters

$typestringrequired
Type of time to retrieve. Accepts 'mysql', 'timestamp', 'U', or PHP date format string (e.g. 'Y-m-d').
$gmtbooloptional
Whether to use GMT timezone.

Default:false

Return

int|string Integer if $type is 'timestamp' or 'U', string otherwise.

Source

function current_time( $type, $gmt = false ) {
	// 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) ( (float) 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 );
}

Changelog

Version Description
5.3.0 Now returns an integer if $type is 'U'. Previously a string was returned.
1.0.0 Introduced.

User Contributed Notes

  1. Skip to note 8 content

    The Date/Time component will be updated in WordPress 5.3, and there are some things that people should be aware of:

    https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/

    From the post:

    Not recommended

    Don’t retrieve time as WP timestamp:

    current_time( 'timestamp' )
    get_post_time( 'U' )

    Don’t localize time based on WP timestamp:

    date_i18n( DATE_RFC3339, $timestamp + $offset )

    Don’t store WP timestamps persistently;
    Don’t compare WP timestamps.

    ——————-

    Recommended

    Retrieve time as Unix timestamp or DateTimeImmutable object:

    time()
    current_datetime()
    get_post_datetime()
    get_post_timestamp()

    Localize time based on Unix timestamp:

    wp_date( DATE_RFC3339, $timestamp )

    Store Unix timestamps or formats that are precise moment in time, such as DATE_RFC3339;
    Compare Unix timestamps, DateTimeInterface objects, or string–comparable dates in same time zone.

  2. Skip to note 9 content

    Examine the results

    ';
    echo "current_time( 'mysql', 1 ) returns GMT: " . current_time( 'mysql', 1 ) . '<br />';
    echo "current_time( 'timestamp' ) returns local site time: " . date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
    echo "current_time( 'timestamp', 1 ) returns GMT: " . date( 'Y-m-d H:i:s', current_time( 'timestamp', 1 ) );
    
    ?>

  3. Skip to note 10 content

    The code snippet gives an Warning with “split” function because
    the function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.

    Alternatives to this function include:

    preg_split()

    On using preg_split(), we get the required output. Code snippet below:

    $blogtime = current_time( 'mysql' );<br />
    list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = preg_split( "([^0-9])", $blogtime );<br />
    echo $hour;

    For reference:
    http://php.net/manual/en/function.split.php

  4. Skip to note 13 content

    When working with time functions, you must use current_time('timestamp') NOT time().

    current_time('timestamp') return blog specific timestamp that is set under Settings->General.
    time() return the time based on date.timezone setting from php.ini.

    Conclusion:
    time() !== current_time('timestamp') // There is a big chance that they are not equal

    Always use: current_time(‘timestamp’)