wppaste
WordPress

current_time( string $type, bool $gmt = false ): int|string

Since
1.0.0, 5.3.0
Source
wp-includes/functions.php:78

Returns the site's current time as a MySQL datetime, custom-formatted string, or timestamp, based on the $type argument. Passing 'timestamp' or 'U' returns an integer that already has the site's GMT offset baked in unless $gmt is true, so it is not a raw Unix timestamp. Any other string is treated as a PHP date() format and applied through DateTime in the site's timezone (or UTC when $gmt is true). Use date_i18n() instead when you need a translated, locale-aware date string for display.

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.

Compatibility

WordPress
since 5.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$typestring
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.Default: false

Return value

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

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Store the current MySQL time in post meta when a price is checked

Post 2 already has a price meta value, so record when it was last verified.

$checked_at = current_time( 'mysql' );
update_post_meta( 2, 'price_checked_at', $checked_at );

printf(
	'Price %s was checked at %s (site time).',
	esc_html( get_post_meta( 2, 'price', true ) ),
	esc_html( get_post_meta( 2, 'price_checked_at', true ) )
);

current_time( 'mysql' ) is already formatted for a DATETIME column, so it can be saved directly without extra conversion.

Compare the site's local time against true GMT time

Call current_time() twice with different $gmt values to see how the same request is reported in two timezones.

$site_time = current_time( 'mysql' );
$gmt_time  = current_time( 'mysql', true );

printf(
	'Site time: %s | GMT time: %s',
	esc_html( $site_time ),
	esc_html( $gmt_time )
);

On a fresh install with no timezone or offset configured, both values will match because the site timezone defaults to UTC.

Common problems and fixes · 3

Why doesn't current_time('timestamp') match time()?

When $gmt is false (the default), the source adds the site's gmt_offset option, converted to seconds, on top of time(). That means the integer it returns is not a real Unix timestamp, it's a shifted value meant for local display math only.

Why is current_time('Y-m-d') showing UTC dates instead of my site's local date?

For any format string other than 'timestamp', 'U', or 'mysql', current_time() builds a DateTime using wp_timezone(), which is derived from the timezone_string or gmt_offset options. If neither is configured, wp_timezone() falls back to UTC, so the output looks 'wrong' even though $gmt was never set to true.

Can I pass any PHP date() format string to current_time()?

Yes, the source only special-cases the literal strings 'mysql', 'timestamp', and 'U'; every other string is handed straight to DateTime::format(). The catch is that if your intended format happens to be exactly 'U' or 'mysql', it gets intercepted and you'll get a timestamp or a fixed 'Y-m-d H:i:s' string instead of your custom format.

Alternatives and related functions

date_i18n
When you need a translated or locale-formatted date string for on-screen display rather than a raw value for storage.
wp_date
When you want a timezone-aware and locale-aware formatted date without going through current_time()'s mysql/timestamp special cases.
time
When you need an actual Unix/UTC timestamp for comparisons or math, since current_time('timestamp') is offset by the site's GMT setting unless $gmt is true.
wp_timezone
When you need the site's DateTimeZone object directly to build your own DateTime calculations instead of a formatted string.

Performance profile

How much work a call to current_time() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8–25

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 43.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
36

36 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()one call below current_time()
  • cacheobject cachewp_cache_get()one call below current_time()
  • serializeserialisationmaybe_unserialize()one call below current_time()

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost current_time() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8–10time()
always16–18time(), get_option()
$type !== "timestamp" && $type !== "U"22–23wp_timezone(), ->format()
$type !== "timestamp" && $type !== "U"24–25->format()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev428–2551 fewer instruction than PHP 8.5
8.5438–255
8.4438–255
8.3438–255
8.2438–255
8.1438–255
7.4438–255

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 4

  • get_option()Retrieves an option value based on an option name.
  • wp_timezone()Retrieves the timezone of the site as a `DateTimeZone` object.
  • DateTimeZone::__construct()
  • DateTime::__construct()

Used by · 36

Show all 36

Source code

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

Changelog

Introduced in 1.0.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.7
Parameter $gmt retyped from int|bool to bool.verified against source
5.3.0
Now returns an integer if $type is 'U'. Previously a string was returned.from the docblock
1.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.