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.
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
$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.
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()?
- Why is current_time('Y-m-d') showing UTC dates instead of my site's local date?
- Can I pass any PHP date() format string to current_time()?
Why doesn't current_time('timestamp') match time()?
Why is current_time('Y-m-d') showing UTC dates instead of my site's local date?
Can I pass any PHP date() format string to current_time()?
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
- Scaling
- Constant
- Instructions
- 8–25
- Plugin surface
- None
- Called by
- 36
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.
36 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 · 36
- 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 36
- 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_link()Inserts a link into the database, or updates an existing link.
- 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 = 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.
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.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.