human_time_diff()返回两个时间间隔的人性化显示效果

目录

描述

译文

判断两个时间标记的差异。

以人类可阅读的格式,如“1小时”、“5分钟”、“两天”,返回时间标记的差异。

原文

Determines the difference between two timestamps.

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

human_time_diff() 描述

用法

<?php human_time_diff$from$to ); ?>

human_time_diff() 用法

参数

$from

(integer) (必填) Unix timestamp from which the difference begins.

默认值: None

$to

(integer) (可选) Unix timestamp to end the time difference. Default becomes time() if not set.

默认值: ''

human_time_diff() 参数

返回值

(string) 

Human readable time difference.

human_time_diff() 返回值

示例

To print an entry's time ("2 days ago"):

<?php echo human_time_diffget_the_time('U'), current_time('timestamp') ) . ' ago'?>


Internationalized version:

<?php printf_x'%s ago''%s = human-readable time difference''your-text-domain' ), human_time_diffget_the_time'U' ), current_time'timestamp' ) ) ); ?>


For comments:

<?php printf_x'%s ago''%s = human-readable time difference''your-text-domain' ), human_time_diffget_comment_time'U' ), current_time'timestamp' ) ) ); ?>

human_time_diff() 示例

历史

添加于 版本: 1.5.0

human_time_diff() 历史

源文件

human_time_diff() 函数的代码位于 wp-includes/formatting.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Determines the difference between two timestamps.
 *
 * The difference is returned in a human readable format such as "1 hour",
 * "5 mins", "2 days".
 *
 * @since 1.5.0
 *
 * @param int $from Unix timestamp from which the difference begins.
 * @param int $to   Optional. Unix timestamp to end the time difference. Default becomes time() if not set.
 * @return string Human readable time difference.
 */
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:="" min="minute" */="" $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;" $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;" $since="sprintf(" _n(="" '%s="" day',="" '%s="" days',="" $days="" ),="" $days="" );="" }="" elseif="" (="" $diff="">< 30="" *="" day_in_seconds="" &&="" $diff="">= WEEK_IN_SECONDS ) {
		$weeks = round( $diff / WEEK_IN_SECONDS );
		if ( $weeks <= 1="" )="" $weeks="1;" $since="sprintf(" _n(="" '%s="" week',="" '%s="" weeks',="" $weeks="" ),="" $weeks="" );="" }="" elseif="" (="" $diff="">< year_in_seconds="" &&="" $diff="">= 30 * DAY_IN_SECONDS ) {
		$months = round( $diff / ( 30 * DAY_IN_SECONDS ) );
		if ( $months <= 1="" )="" $months="1;" $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;" $since="sprintf(" _n(="" '%s="" year',="" '%s="" years',="" $years="" ),="" $years="" );="" }="" *="" *="" filter="" 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="" );="" }="">

human_time_diff() 源文件

相关