返回介绍

human_time_diff()

发布于 2017-09-11 00:55:59 字数 5945 浏览 1083 评论 0 收藏 0

human_time_diff( int $from,  int $to = '' )

Determines the difference between two timestamps.


description

The difference is returned in a human readable format such as "1 hour", "5 mins", "2 days".


参数

$from

(int) (Required) Unix timestamp from which the difference begins.

$to

(int) (Optional) Unix timestamp to end the time difference. Default becomes time() if not set.

Default value: ''


返回值

(string) Human readable time difference.


源代码

File: wp-includes/formatting.php

function human_time_diff( $from, $to = '' ) {
	if ( empty( $to ) ) {
		$to = time();
	}

	$diff = (int) abs( $to - $from );

	if ( $diff < HOUR_IN_SECONDS ) {
		$mins = round( $diff / MINUTE_IN_SECONDS );
		if ( $mins <= 1 )
			$mins = 1;
		/* translators: Time difference between two dates, in minutes (min=minute). 1: Number of minutes */
		$since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
	} elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) {
		$hours = round( $diff / HOUR_IN_SECONDS );
		if ( $hours <= 1 )
			$hours = 1;
		/* translators: Time difference between two dates, in hours. 1: Number of hours */
		$since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
	} elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) {
		$days = round( $diff / DAY_IN_SECONDS );
		if ( $days <= 1 )
			$days = 1;
		/* translators: Time difference between two dates, in days. 1: Number of days */
		$since = sprintf( _n( '%s day', '%s days', $days ), $days );
	} elseif ( $diff < MONTH_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) {
		$weeks = round( $diff / WEEK_IN_SECONDS );
		if ( $weeks <= 1 )
			$weeks = 1;
		/* translators: Time difference between two dates, in weeks. 1: Number of weeks */
		$since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks );
	} elseif ( $diff < YEAR_IN_SECONDS && $diff >= MONTH_IN_SECONDS ) {
		$months = round( $diff / MONTH_IN_SECONDS );
		if ( $months <= 1 )
			$months = 1;
		/* translators: Time difference between two dates, in months. 1: Number of months */
		$since = sprintf( _n( '%s month', '%s months', $months ), $months );
	} elseif ( $diff >= YEAR_IN_SECONDS ) {
		$years = round( $diff / YEAR_IN_SECONDS );
		if ( $years <= 1 )
			$years = 1;
		/* translators: Time difference between two dates, in years. 1: Number of years */
		$since = sprintf( _n( '%s year', '%s years', $years ), $years );
	}

	/**
	 * Filters the human readable difference between two timestamps.
	 *
	 * @since 4.0.0
	 *
	 * @param string $since The difference in human readable text.
	 * @param int    $diff  The difference in seconds.
	 * @param int    $from  Unix timestamp from which the difference begins.
	 * @param int    $to    Unix timestamp to end the time difference.
	 */
	return apply_filters( 'human_time_diff', $since, $diff, $from, $to );
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: human_time_diff
  • wp-includes/l10n.php: _n()
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-admin/includes/class-wp-posts-list-table.php: WP_Posts_List_Table::column_date()
  • wp-admin/includes/class-wp-media-list-table.php: WP_Media_List_Table::column_date()
  • wp-admin/includes/plugin-install.php: install_plugin_information()
  • wp-admin/includes/class-wp-plugin-install-list-table.php: WP_Plugin_Install_List_Table::display_rows()
  • wp-admin/includes/revision.php: wp_prepare_revisions_for_js()
  • wp-includes/post-template.php: wp_post_revision_title_expanded()
  • Show 1 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

    To print an entry’s time (“2 days ago”):

    
    <?php echo esc_html( human_time_diff( get_the_time('U'), current_time('timestamp') ) ) . ' ago'; ?>
    
  2. Internationalized version:

    
    printf( _x( '%1$s ago', '%2$s = human-readable time difference', 'wpdocs_textdomain' ),
    	human_time_diff( get_the_time( 'U' ),
    	current_time( 'timestamp' )
    );
    

    For comments:

    
    printf( _x( '%1$s ago', '%2$s = human-readable time difference', 'wpdocs_textdomain' ),
    	human_time_diff( get_comment_time( 'U' ),
    	current_time( 'timestamp' )
    );
    
    

    Get posted and last modified time

    
    <?php
    $lastmodified = get_the_modified_time('U');
    $posted = get_the_time('U');
    echo "Posted " . human_time_diff($posted,current_time( 'U' )). "ago";
    echo "</br>";
      if ($lastmodified > $posted) {
    	  echo "Edited " . human_time_diff($lastmodified,current_time('U')) . " ago";   
      } 
    ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文