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 ) ? date( $type ) : date( $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 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 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

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