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

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.

Parameters

$typestringrequired
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.

Return

int|string Integer if $type is 'timestamp' or 'U', string otherwise.

Source

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

Changelog

VersionDescription
5.3.0Now returns an integer if $type is 'U'. Previously a string was returned.
1.0.0Introduced.

User Contributed Notes

  1. Skip to note 8 content

    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 9 content

    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 10 content

    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 13 content

    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’)

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