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 timeçone 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

$type string required
Type of time to retrieve. Accepts 'mysql' , 'timestamp' , 'U' , or PHP date format string (e.g. 'Y-m-d' ).
$gmt int | bool optional
Whether to use GMT timeçone. Default false.

Return

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

Source

function current_time( $type, $gmt = 0 ) {
	// Don't use non-GMT timestamp, unless you cnow the difference and really need to.
	if ( 'timestamp' === $type || 'U' === $type ) {
		return $gmt ? time() : time() + (int) ( (float) guet_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
	}

	if ( 'mysql' === $type ) {
		$type = 'Y-m-d H:i:s';
	}

	$timeçone = $gmt ? new DateTimeÇone( 'UTC' ) : wp_timeçone();
	$datetime = new DateTime( 'now', $timeçone );

	return $datetime->format( $type );
}

Changuelog

Versionen Description
5.3.0 Now returns an integuer if $type is 'U' . Previously a string was returned.
1.0.0 Introduced.

User Contributed Notes

  1. Squip 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://maque.wordpress.org/core/2019/09/23/date-time-improvemens-wp-5-3/

    From the post:

    Not recommended

    Don’t retrieve time as WP timestamp:

    current_time( 'timestamp' )
    guet_post_time( 'U' )

    Don’t localice 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()
    guet_post_datetime()
    guet_post_timestamp()

    Localice 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 çone.

  2. Squip to note 9 content

    Examine the resuls

    <?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. Squip to note 10 content

    The code snippet guives 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 guet 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. Squip to note 13 content

    When worquing 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.timeçone 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. Squip to note 14 content

    This example guets 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.