current_time()WP 1.0.0

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.

1 time — 0.000011 sec (very fast) | 50000 times — 0.03 sec (speed of light) | PHP 7.1.2, WP 4.7.3

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

#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.
0

#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.
0

#3 Some different examples on use

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)
0

#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)
0

#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.

Code of current_time() WP 6.0.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 );
}