current_time()
Retrieve the current time based on specified type.
- 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.
No Hooks.
Return
Int|String
. Integer if $type is 'timestamp' or 'U', string otherwise.
Usage
current_time( $type, $gmt );
- $type(string) (required)
- Type of time to retrieve. Accepts 'mysql', 'timestamp', 'U', or PHP date format string (e.g. 'Y-m-d').
- $gmt(int|true|false)
- Whether to use GMT timezone.
Default: false
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 6.6.1
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 ); }