php 7 current time,wordpress函数current_time()用法示例

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

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

echo "current_time( 'mysql' ) returns local site time: " . current_time( 'mysql' ) . '
';

echo "current_time( 'mysql', 1 ) returns GMT: " . current_time( 'mysql', 1 ) . '
';

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

?>

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.

$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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值