WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/current time

Description

Returns the blog's current local time in the specified format. There are two named formats: 'mysql' for MySQL/MariaDB's timestamp data type format (i.e. YYYY-MM-DD HH:MM:SS), and 'timestamp' for the Unix timestamp format (i.e. epoch). Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d') since 3.9.0. The optional secondary parameter can be used to retrieve GMT time instead of the blog's local time.

The local time returned is based on the timezone set on the blog's General Settings page, which is UTC by default.

Note that if you are storing the time of an event, current_time( 'timestamp' ) should not be used in lieu of time(), as it returns a Unix timestamp that is incorrectly offset and inaccurate. Unix timestamps are always in UTC and do not have any other timezone attached. Should the site owner change the timezone of the WordPress installation at any time, stored timestamps that are offset using current_time( 'timestamp' ) will be incorrect. Instead of using current_time( 'timestamp' ), which is no longer recommended, use time() and store it unmodified. Then upon output to the screen, convert it to the user's timezone by passing the timezone to your visualization method using PHP's DateTime class.

Usage

<?php $time current_time$type$gmt ); ?>

Parameters

$type
(string) (required) The time format to return. Possible values:
  • mysql
  • timestamp
  • A PHP date format (e.g. 'Y-m-d') (since 3.9)
Default: None
$gmt
(integer) (optional) The time zone (GMT, local) of the returned time: Possible values:
  • 1
  • 0
Default: 0

Returns

If the first parameter is 'mysql', the function returns a date-time string. If the first parameter is 'timestamp', the function returns a double value equal to the number of seconds since Jan. 1, 1970. When strict data typing is necessary, take note that the PHP time() function, which current_time() replaces, returns an integer value, so consider using (int) current_time( 'timestamp' ) instead.

As of version 3.9, any other value for the first parameter will be used as the format for the time. For example, current_time( 'Y-m-d' ) will return something like '2014-04-14'.

If the optional second parameter is 1, the value returned represents the current GMT time. If 0 or no second parameter are set, the value returned represents the local time for the timezone declared in the blog's Timezone setting on the General Settings page.

Example

This example gets the current time and assigns the parameters to variables.

<?php 
$blogtime = current_time( 'mysql' ); 
list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = preg_split( '([^0-9])', $blogtime );
?>
Example of format of current_time( 'mysql' ):
   2005-08-05 10:41:13

Examine the results

<?php

echo "current_time( 'mysql' ) returns local site time: " . current_time( 'mysql' ) . '<br />';
echo "current_time( 'mysql', 1 ) returns GMT: " . current_time( 'mysql', 1 ) . '<br />';
echo "current_time( 'timestamp' ) returns local site time: " . date( 'Y-m-d H:i:s', current_time( 'timestamp', 0 ) );
echo "current_time( 'timestamp', 1 ) returns GMT: " . date( 'Y-m-d H:i:s', current_time( 'timestamp', 1 ) );

?>

Notes

Change Log

  • Since: 1.0
  • 3.9.0: $type can now be any PHP date format.

Source File

current_time() is located in wp-includes/functions.php

Related

Time Functions: current_time(), human_time_diff(), the_time(), get_the_time(), comment_time()

See also index of Function Reference and index of Template Tags.