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

Retrieve 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. 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 ) {
	switch ( $type ) {
		case 'mysql':
			return ( $gmt ) ? gmdate( 'Y-m-d H:i:s' ) : gmdate( 'Y-m-d H:i:s', ( time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
		case 'timestamp':
			return ( $gmt ) ? time() : time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
		default:
			return ( $gmt ) ? gmdate( $type ) : gmdate( $type, time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
	}
}

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 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 ) );
    
    ?>
    
    
  2. Skip to note 2 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

  3. Skip to note 4 content
    Contributed by Andrei 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’)

  4. Skip to note 5 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.