# human_time_diff()

URL: https://chugunov.pro/api-wordpress/functions/human_time_diff/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 1.5.0.

## Сигнатура

```php
human_time_diff( int $from, int $to ): string
```

## Описание

Разница возвращается в удобном для чтения формате, например «1 час», «5 минут», «2 дня».

## Параметры

- `$from` `int` — обязательный. Временная метка Unix, с которой начинается отсчёт разницы.
- `$to` `int` — необязательный. Временная метка Unix, на которой заканчивается отсчёт разницы. Если не задана, по умолчанию используется time().

## Возвращаемое значение

`string`

## Исходный код

Файл: `wp-includes/formatting.php:3871`

```php
function human_time_diff( $from, $to = 0 ) {
	if ( empty( $to ) ) {
		$to = time();
	}

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

	if ( $diff < MINUTE_IN_SECONDS ) {
		$secs = $diff;
		if ( $secs <= 1 ) {
			$secs = 1;
		}
		/* translators: Time difference between two dates, in seconds. %s: Number of seconds. */
		$since = sprintf( _n( '%s second', '%s seconds', $secs ), $secs );
	} elseif ( $diff < HOUR_IN_SECONDS && $diff >= MINUTE_IN_SECONDS ) {
		$mins = round( $diff / MINUTE_IN_SECONDS );
		if ( $mins <= 1 ) {
			$mins = 1;
		}
		/* translators: Time difference between two dates, in minutes. %s: Number of minutes. */
		$since = sprintf( _n( '%s minute', '%s minutes', $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. %s: 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. %s: 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. %s: 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. %s: 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. %s: 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 );
}
```

## История изменений

- 5.3.0 — Added support for showing a difference in seconds.
- 1.5.0 — Introduced.

## Связанные

Использует: [`_n`](https://chugunov.pro/api-wordpress/functions/_n/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/).
Используется в: `WP_REST_Block_Directory_Controller::prepare_item_for_response`, [`wp_get_auto_update_message`](https://chugunov.pro/api-wordpress/functions/wp_get_auto_update_message/), `WP_Recovery_Mode_Email_Service::maybe_send_recovery_mode_email`, `WP_Recovery_Mode_Email_Service::send_recovery_mode_email`, `WP_Privacy_Requests_Table::get_timestamp_as_date`, `WP_Media_List_Table::column_date`, [`install_plugin_information`](https://chugunov.pro/api-wordpress/functions/install_plugin_information/), `WP_Plugin_Install_List_Table::display_rows`, [`wp_prepare_revisions_for_js`](https://chugunov.pro/api-wordpress/functions/wp_prepare_revisions_for_js/), [`wp_post_revision_title_expanded`](https://chugunov.pro/api-wordpress/functions/wp_post_revision_title_expanded/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/human_time_diff/
