current_time( string $type, int|bool $gmt )

Retrieves the current time based on specified type.


Description Description

The ‘mysql’ type will return the time in the format for MySQL DATETIME field. The ‘timestamp’ type 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 set to either ‘1’ or ‘true’, then both types will use GMT time. if $gmt is false, the output is adjusted with the GMT offset in the WordPress option.


Parameters Parameters

$type

(string) (Required) Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date format string (e.g. 'Y-m-d').

$gmt

(int|bool) (Optional) Whether to use GMT timezone. Default false.


Top ↑

Return Return

(int|string) Integer if $type is 'timestamp', string otherwise.


Top ↑

Source Source

File: wp-includes/functions.php

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

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by dingo-d

    The Date/Time component will be updated in WordPress 5.3, and there are some things that people should be aware of:

    https://make.wordpress.org/core/2019/09/23/date-time-improvements-wp-5-3/

    From the post:

    Not recommended

    Don’t retrieve time as WP timestamp:

    current_time( 'timestamp' )
    get_post_time( 'U' )

    Don’t localize time based on WP timestamp:

    date_i18n( DATE_RFC3339, $timestamp + $offset )

    Don’t store WP timestamps persistently;
    Don’t compare WP timestamps.

    ——————-

    Recommended

    Retrieve time as Unix timestamp or DateTimeImmutable object:

    time()
    current_datetime()
    get_post_datetime()
    get_post_timestamp()

    Localize time based on Unix timestamp:

    wp_date( DATE_RFC3339, $timestamp )

    Store Unix timestamps or formats that are precise moment in time, such as DATE_RFC3339;
    Compare Unix timestamps, DateTimeInterface objects, or string–comparable dates in same time zone.

  2. Skip to note 2 content
    Contributed by Codex

    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 ) );
    
    ?>
    
    
  3. Skip to note 3 content
    Contributed by kapils003

    The code snippet gives an Warning with “split” function because
    the function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.

    Alternatives to this function include:

    preg_split()

    On using preg_split(), we get the required output. Code snippet below:

    $blogtime = current_time( 'mysql' );
    list( $today_year, $today_month, $today_day, $hour, $minute, $second ) = preg_split( "([^0-9])", $blogtime );
    echo $hour;

    For reference:
    http://php.net/manual/en/function.split.php

  4. Skip to note 5 content
    Contributed by Andrew Surdu

    When working with time functions, you must use current_time('timestamp') NOT time().

    current_time('timestamp') return blog specific timestamp that is set under Settings->General.
    time() return the time based on date.timezone setting from php.ini.

    Conclusion:
    time() !== current_time('timestamp') // There is a big chance that they are not equal

    Always use: current_time(‘timestamp’)

  5. Skip to note 6 content
    Contributed by Codex

    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 ) = split( '([^0-9])', $blogtime );
    ?>
    

    Example of format of current_time( 'mysql' ):

    2005-08-05 10:41:13

You must log in before being able to contribute a note or feedback.