current_time() - WordPress 函数参考中文文档

返回介绍

current_time()

发布于 2017-09-10 字数 6747 浏览 851 评论 0

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

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.


参数

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


返回值

(int|string) Integer if $type is ‘timestamp’, string otherwise.


源代码

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

更新日志

Version 描述
1.0.0 Introduced.

相关函数

Uses

  • wp-includes/option.php:
    get_option()

Used By

  • wp-admin/includes/class-wp-community-events.php:
    WP_Community_Events::trim_events()
  • wp-includes/class-wp-customize-manager.php:
    WP_Customize_Manager::save_changeset_post()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php:
    WP_REST_Comments_Controller::create_item()
  • wp-includes/functions.php:
    _wp_upload_dir()
  • wp-admin/includes/schema.php:
    populate_network()
  • wp-admin/includes/dashboard.php:
    wp_dashboard_recent_posts()
  • wp-admin/includes/upgrade.php:
    wp_install_defaults()
  • wp-admin/includes/template.php:
    touch_time()
  • wp-admin/includes/media.php:
    media_handle_upload()
  • wp-admin/includes/media.php:
    media_handle_sideload()
  • wp-admin/includes/ajax-actions.php:
    wp_ajax_add_meta()
  • wp-admin/includes/meta-boxes.php:
    post_submit_meta_box()
  • wp-includes/general-template.php:
    get_calendar()
  • wp-includes/class-wp-query.php:
    WP_Query::get_posts()
  • wp-includes/functions.php:
    date_i18n()
  • wp-includes/link-template.php:
    get_month_link()
  • wp-includes/link-template.php:
    get_day_link()
  • wp-includes/link-template.php:
    get_year_link()
  • wp-includes/date.php:
    WP_Date_Query::build_mysql_datetime()
  • wp-includes/post-template.php:
    wp_post_revision_title_expanded()
  • wp-includes/post.php:
    wp_insert_post()
  • wp-includes/post.php:
    wp_update_post()
  • wp-includes/ms-functions.php:
    wpmu_log_new_registrations()
  • wp-includes/ms-functions.php:
    wpmu_activate_signup()
  • wp-includes/ms-functions.php:
    insert_blog()
  • wp-includes/ms-functions.php:
    wpmu_validate_user_signup()
  • wp-includes/ms-functions.php:
    wpmu_validate_blog_signup()
  • wp-includes/ms-functions.php:
    wpmu_signup_blog()
  • wp-includes/ms-functions.php:
    wpmu_signup_user()
  • wp-includes/ms-blogs.php:
    update_blog_status()
  • wp-includes/ms-blogs.php:
    update_blog_details()
  • wp-includes/ms-blogs.php:
    wpmu_update_blogs_date()
  • wp-includes/class-wp-xmlrpc-server.php:
    wp_xmlrpc_server::blogger_newPost()
  • wp-includes/comment.php:
    wp_new_comment()
  • wp-includes/comment.php:
    wp_insert_comment()
  • Show 30 more used by
    Hide more used by


User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note

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

    PHP date formats accepted for $type are defined at http://php.net/manual/en/function.date.php#refsect1-function.date-parameters

    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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!