current_time() WordPress Function

The current_time() function is used to get the current time as a formatted string. The function accepts two arguments: the first is a string containing the format for the time, and the second is an integer representing the time zone. The default time zone is the server's default time zone.

current_time( string $type, int|bool $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.


Top ↑

Parameters

$type

(string)(Required)Type of time to retrieve. Accepts 'mysql', 'timestamp', 'U', or PHP date format string (e.g. 'Y-m-d').

$gmt

(int|bool)(Optional) Whether to use GMT timezone. Default false.


Top ↑

Return

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


Top ↑

Source

File: wp-includes/functions.php

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 );
}


Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More