wppaste
WordPress

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

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

Returns the current site date or time, formatted according to the $type you pass in ('mysql', 'timestamp', 'U', or any PHP date format string). The $gmt argument decides whether the value is calculated in UTC or adjusted to the site's configured timezone via wp_timezone(). A common trap is treating the 'timestamp'/'U' result as a real Unix timestamp when $gmt is left false, since the source deliberately adds the site's gmt_offset to time() in that case. For genuinely UTC-safe values or localized display strings, pair this with time() or date_i18n().

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').
$gmtint|booloptional
Whether to use GMT timezone. Default false.Default: 0

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.

Get the current site time in MySQL DATETIME format

Print the site's local and GMT time as MySQL-ready strings, plus a custom PHP date format.

$mysql_time = current_time( 'mysql' );
echo esc_html( 'Site local time (mysql format): ' . $mysql_time ) . "\n";

$gmt_mysql_time = current_time( 'mysql', true );
echo esc_html( 'GMT time (mysql format): ' . $gmt_mysql_time ) . "\n";

$formatted = current_time( 'F j, Y g:i a' );
echo esc_html( 'Custom PHP date format: ' . $formatted );

The plain 'Y-m-d'-style format falls through to DateTime::format(), so any valid PHP date() character works, not just 'mysql'.

Record a 'last viewed' timestamp on a post

Store a timestamp in post meta on an existing post and read it back for display.

$post_id = 2;
$now_timestamp = current_time( 'timestamp' );
update_post_meta( $post_id, 'last_viewed', $now_timestamp );

$stored_timestamp = (int) get_post_meta( $post_id, 'last_viewed', true );
$readable = gmdate( 'Y-m-d H:i:s', $stored_timestamp );

echo esc_html( sprintf( 'Post #%d was marked viewed at site time %s (stored timestamp %d).', $post_id, $readable, $stored_timestamp ) );

current_time( 'timestamp' ) without $gmt is offset for display purposes, so pass true if the stored value needs to be directly comparable with time().

Common problems and fixes · 3

Why is current_time('timestamp') off by several hours compared to time()?

The source explicitly adds the site's gmt_offset option, converted to seconds, to time() whenever $gmt is falsey. It's not a bug, it's meant for display math using the site's local time, not for real UTC comparisons.

Why does current_time('mysql') change after I edit the site's timezone setting?

When $gmt is false, the function builds its DateTime using wp_timezone(), which reads the timezone_string or gmt_offset general setting. Changing that setting changes every non-GMT call immediately.

Can I pass any date format string to current_time?

Only 'mysql', 'timestamp', and 'U' are special-cased in the source. Anything else is handed straight to DateTime::format(), so it must be a valid PHP date() format string or you'll get garbage or literal characters back.

Alternatives and related functions

time
When you need the real, unmodified Unix timestamp with no site-offset math applied, call time() directly instead of current_time('timestamp').
date_i18n
When you need a translated, human-readable date or time string for display, use date_i18n() which also accepts a $gmt flag.
wp_date
When you need a timezone-aware, translatable date string tied to a specific DateTimeZone rather than just the site default, use wp_date() instead.
wp_timezone
When you only need the site's configured DateTimeZone object to build your own DateTime logic, call wp_timezone() rather than parsing current_time()'s output.

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
35

35 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 · 35

Show all 35

Source code

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) ( (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.7.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.