1. 首页
  2. WordPress 函数手册

human_time_diff()

human_time_diff( int $from, int $to )

确定两个时间戳之间的差异。
Determines the difference between two timestamps.

目录锚点:#说明#参数#返回#源码#笔记


说明(Description)

差异以人类可读的格式返回,例如“1小时”、“5分钟”、“2天”。


参数(Parameters)

参数 类型 必填 说明
$from (int) 必需 差异开始的Unix时间戳。
$to (int) 可选 结束时差的Unix时间戳。如果未设置,默认值将变为time()。

返回(Return)

(string)人类可读的时差。


源码(Source)

/**
 * 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="" );="" }="">
更新版本 源码位置 使用 被使用
5.3.0 wp-includes/formatting.php:3710 9 3

笔记(Notes)

打印条目的时间(“2天前”):
发布和上次修改时间
国际化版本:

human_time_diff() 为WP2原创文章,链接:https://www.wp2.cn/functions/human_time_diff/

发表评论

您的电子邮箱地址不会被公开。