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
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 );
}
Skip to note 8 content
Denis Žoljom
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:
Don’t store WP timestamps persistently;
Don’t compare WP timestamps.
——————-
Recommended
Retrieve time as Unix timestamp or
DateTimeImmutableobject:Localize time based on Unix timestamp:
Store Unix timestamps or formats that are precise moment in time, such as
DATE_RFC3339;Compare Unix timestamps,
DateTimeInterfaceobjects, or string–comparable dates in same time zone.Skip to note 9 content
Codex
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 ) ); ?>Skip to note 10 content
kapils003
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
Skip to note 11 content
carlosonweb
Since version 5.3, the code below is discouraged as it will not return a Unix (UTC) timestamp.
current_time( 'timestamp' );Here’s the alternative solution:
Skip to note 12 content
jon
PHP date formats accepted for $type are defined at http://php.net/manual/en/function.date.php#refsect1-function.date-parameters
Skip to note 13 content
Andrei Surdu
When working with time functions, you must use
current_time('timestamp')NOTtime().current_time('timestamp')return blog specific timestamp that is set under Settings->General.time()return the time based ondate.timezonesetting from php.ini.Conclusion:
time() !== current_time('timestamp') // There is a big chance that they are not equalAlways use: current_time(‘timestamp’)
Skip to note 14 content
Codex
This example gets the current time and assigns the parameters to variables.
Example of format of
current_time( 'mysql' ):2005-08-05 10:41:13