current_time()
Returns the current site time, taking into account the site's timezone setting (timezone). The format in which to return the result can be specified arbitrarily. mysql: YYYY-MM-DD HH:MM:SS, unix: 1335820033, arbitrary format: d. M. Y.
You can use the second parameter to get the time in GMT/UTC instead of as the site's local time.
Local time will be obtained according to the established global site settings about the timezone (option gmt_offset).
Use current_time('timestamp') instead of time(), when you need to get the site time considering the timezone.
Starting from version 5.3, it works based on PHP classes: DateTime{} and DateTimeZone{}.
The default server time is set in the ini option date.timezone, and can also be set by the function date_default_timezone_set(), which has a higher priority over the ini option.
WP sets the default server timezone to UTC at a very early stage of core loading in the file wp-settings.php:
date_default_timezone_set( 'UTC' );
To find out the current server timezone, you can use date_default_timezone_get():
echo date_default_timezone_get(); // Europe/Moscow
Also read: Date/Time in WordPress.
No Hooks.
Returns
Int|String. A string of time in the specified format.
Usage
current_time( $type, $gmt );
- $type(string) (required)
- The format in which the function will return the time. Can be:
mysql,timestampor arbitrary format like:Y-m-d H:i:s. All format options. - $gmt(number)
The timezone (GMT, local) of the returned time. Can be:
1- the time will be returned in GMT zone.0- will return the site's time set in the main settings.
Default: 0 (site time)
Examples
#1 New in WordPress 5.3
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/
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 Get the current time and break it down into its components (days, hours, minutes):
$blogtime = current_time('mysql'); // return: 2005-08-05 10:41:13
list( $year, $month, $day, $hour, $minute, $second ) = preg_split( '([^0-9])', $blogtime );
// Now we have variables:
// $year is the current year
// $month is the current month
// etc. #3 Demo
If we use this code somewhere in our code:
echo current_time( 'mysql' ); // 2012-04-30 21:48:07 - server time (SQL format). echo current_time( 'mysql', 1 ); // 2012-04-30 17:48:07 - time in GMT/UTC zone (SQL format). echo current_time( 'timestamp' ); // 1335822487 - server time (Unix format). echo current_time( 'timestamp', 1 ); // 1335808087 - time in GMT/UTC zone (Unix format). var_dump( current_time( 'timestamp' ) ); // int(1625452800)
If the site's time differs from UTC, then the "timestamp" will return the time stamp + the time zone offset set in the settings. That is, the result will differ from what the time() function returns:
var_dump( current_time( 'timestamp' ) ); //> int(1711046478) var_dump( time() ); //> int(1711028478)
#4 Display the current date and time of the site in the desired format
echo current_time('d m Y H:i');
//> 17 07 2016 15:51 (taking into account the site time zone) #5 Note about timestamp
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:
$local_time = current_datetime(); $current_time = $local_time->getTimestamp() + $local_time->getOffset();
Changelog
| Since 1.0.0 | Introduced. |
| Since 5.3.0 | Now returns an integer if $type is 'U'. Previously a string was returned. |
current_time() current time code WP 7.0
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 );
}