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

Determines the difference between two timestamps.


Description Description

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


Parameters Parameters

$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: ''


Top ↑

Return Return

(string) Human readable time difference.


Top ↑

Source Source

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

Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by kommasri

    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";   
      } 
    ?>
    

You must log in before being able to contribute a note or feedback.