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().
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
$typeis '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()?
- Why does current_time('mysql') change after I edit the site's timezone setting?
- Can I pass any date format string to current_time?
Why is current_time('timestamp') off by several hours compared to time()?
Why does current_time('mysql') change after I edit the site's timezone setting?
Can I pass any date format string to current_time?
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
- Scaling
- Constant
- Instructions
- 8–25
- Plugin surface
- None
- Called by
- 35
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 43.
Nothing here hands control to plugin code.
35 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()one call below current_time() - cacheobject cache
wp_cache_get()one call below current_time() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–10 | time() |
| always | 16–18 | time(), get_option() |
$type !== "timestamp" && $type !== "U" | 22–23 | wp_timezone(), ->format() |
$type !== "timestamp" && $type !== "U" | 24–25 | ->format() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 42 | 8–25 | 5 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 43 | 8–25 | 5 | |
| 8.4 | 43 | 8–25 | 5 | |
| 8.3 | 43 | 8–25 | 5 | |
| 8.2 | 43 | 8–25 | 5 | |
| 8.1 | 43 | 8–25 | 5 | |
| 7.4 | 43 | 8–25 | 5 |
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
- WP_Customize_Manager::customize_pane_settings()Prints JavaScript settings for parent window.
- WP_Customize_Manager::save_changeset_post()Saves the post for the loaded changeset.
- WP_Date_Query::build_mysql_datetime()Builds a MySQL format date/time based on some query parameters.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_REST_Comments_Controller::create_item()Creates a comment.
- _wp_upload_dir()A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path.
- bulk_edit_posts()Processes the post data for the bulk editing of posts.
- date_i18n()Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.
- get_calendar()Displays calendar with days that have posts as links.
- get_day_link()Retrieves the permalink for the day archives with year and month.
- get_month_link()Retrieves the permalink for the month archives with year.
- get_year_link()Retrieves the permalink for the year archives.
Show all 35
- inject_ignored_hooked_blocks_metadata_attributes()Inject ignoredHookedBlocks metadata attributes into a template or template part.
- media_handle_sideload()Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
- media_handle_upload()Saves a file submitted from a POST request and create an attachment post for it.
- populate_network()Populate network settings.
- post_submit_meta_box()Displays post submit form fields.
- touch_time()Prints out HTML form date elements for editing post or comment publish date.
- wp_create_user_request()Creates and logs a user request to perform a specific action.
- wp_dashboard_recent_posts()Generates Publishing Soon and Recently Published sections.
- wp_insert_comment()Inserts a comment into the database.
- wp_insert_post()Inserts or update a post.
- wp_insert_site()Inserts a new site into the database.
- wp_install_defaults()Creates the initial content for a newly-installed site.
- wp_new_comment()Adds a new comment to the database.
- wp_privacy_generate_personal_data_export_file()Generate the personal data export file.
- wp_resolve_post_date()Uses wp_checkdate to return a valid Gregorian-calendar value for post_date.
- wp_update_post()Updates a post with new post data.
- wp_update_site()Updates a site in the database.
- wp_xmlrpc_server::blogger_newPost()Creates a new post.
- wpmu_activate_signup()Activates a signup.
- wpmu_log_new_registrations()Logs the user email, IP, and registration date of a new site.
- wpmu_signup_blog()Records site signup information for future activation.
- wpmu_signup_user()Records user signup information for future activation.
- wpmu_update_blogs_date()Updates the last_updated field for the current site.
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.
Signature, return type and hooks compared across 5 parsed releases.
$gmt retyped from int|bool to bool.verified against source$type is 'U'. Previously a string was returned.from the docblockAbout 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.