current_time

The timeline below displays how wordpress function current_time has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.9

/**
 * Retrieves the current time based on specified type.
 *
 *  - 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.
 *
 * @since 1.0.0
 * @since 5.3.0 Now returns an integer if `$type` is 'U'. Previously a string was returned.
 *
 * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp', 'U',
 *                       or PHP date format string (e.g. 'Y-m-d').
 * @param int|bool $gmt  Optional. Whether to use GMT timezone. Default false.
 * @return int|string Integer if `$type` is 'timestamp' or 'U', string otherwise.
 */
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);
}

WordPress Version: 5.3

/**
 * Retrieves the current time based on specified type.
 *
 * 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.
 *
 * @since 1.0.0
 *
 * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp',
 *                       or PHP date format string (e.g. 'Y-m-d').
 * @param int|bool $gmt  Optional. Whether to use GMT timezone. Default false.
 * @return int|string Integer if $type is 'timestamp', string otherwise.
 */
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);
}

WordPress Version: 5.1

/**
 * Retrieve the current time based on specified type.
 *
 * 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.
 *
 * @since 1.0.0
 *
 * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date
 *                       format string (e.g. 'Y-m-d').
 * @param int|bool $gmt  Optional. Whether to use GMT timezone. Default false.
 * @return int|string Integer if $type is 'timestamp', string otherwise.
 */
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);
    }
}

WordPress Version: 4.0

/**
 * Retrieve the current time based on specified type.
 *
 * 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.
 *
 * @since 1.0.0
 *
 * @param string   $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date
 *                       format string (e.g. 'Y-m-d').
 * @param int|bool $gmt  Optional. Whether to use GMT timezone. Default false.
 * @return int|string Integer if $type is 'timestamp', string otherwise.
 */
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);
    }
}

WordPress Version: 3.9

/**
 * Retrieve the current time based on specified type.
 *
 * 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.
 *
 * @since 1.0.0
 *
 * @param string $type 'mysql', 'timestamp', or PHP date format string (e.g. 'Y-m-d').
 * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false.
 * @return int|string String if $type is 'gmt', int if $type is 'timestamp'.
 */
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);
            break;
        case 'timestamp':
            return $gmt ? time() : (time() + get_option('gmt_offset') * HOUR_IN_SECONDS);
            break;
        default:
            return $gmt ? date($type) : date($type, time() + get_option('gmt_offset') * HOUR_IN_SECONDS);
            break;
    }
}

WordPress Version: 3.7

/**
 * Retrieve the current time based on specified type.
 *
 * The 'mysql' type will return the time in the format for MySQL DATETIME field.
 * The 'timestamp' type will return the current timestamp.
 *
 * 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.
 *
 * @since 1.0.0
 *
 * @param string $type Either 'mysql' or 'timestamp'.
 * @param int|bool $gmt Optional. Whether to use GMT timezone. Default is false.
 * @return int|string String if $type is 'gmt', int if $type is 'timestamp'.
 */
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);
            break;
        case 'timestamp':
            return $gmt ? time() : (time() + get_option('gmt_offset') * HOUR_IN_SECONDS);
            break;
    }
}