llms_get_date_diff( mixed $time1, mixed $time2, integer $precision = 2 )

Get human readable time difference between 2 dates


Description Description

Return difference between 2 dates in year, month, hour, minute or second The $precision caps the number of time units used: for instance if $time1 – $time2 = 3 days, 4 hours, 12 minutes, 5 seconds

  • with precision = 1 : 3 days
  • with precision = 2 : 3 days, 4 hours
  • with precision = 3 : 3 days, 4 hours, 12 minutes.

Top ↑

Parameters Parameters

$time1

(mixed) (Required) A time (string or timestamp).

$time2

(mixed) (Required) A time (string or timestamp).

$precision

(integer) (Optional) precision. Default is 2.

Default value: 2


Top ↑

Return Return

(string) time difference


Top ↑

Source Source

File: includes/llms.functions.core.php

 */
function llms_get_date_diff( $time1, $time2, $precision = 2 ) {
	// If not numeric then convert timestamps.
	if ( ! is_numeric( $time1 ) ) {
		$time1 = strtotime( $time1 );
	}
	if ( ! is_numeric( $time2 ) ) {
		$time2 = strtotime( $time2 );
	}
	// If time1 > time2 then swap the 2 values.
	if ( $time1 > $time2 ) {
		list( $time1, $time2 ) = array( $time2, $time1 );
	}
	// Set up intervals and diffs arrays.
	$intervals     = array( 'year', 'month', 'day', 'hour', 'minute', 'second' );
	$l18n_singular = array(
		'year'   => __( 'year', 'lifterlms' ),
		'month'  => __( 'month', 'lifterlms' ),
		'day'    => __( 'day', 'lifterlms' ),
		'hour'   => __( 'hour', 'lifterlms' ),
		'minute' => __( 'minute', 'lifterlms' ),
		'second' => __( 'second', 'lifterlms' ),
	);
	$l18n_plural   = array(
		'year'   => __( 'years', 'lifterlms' ),
		'month'  => __( 'months', 'lifterlms' ),
		'day'    => __( 'days', 'lifterlms' ),
		'hour'   => __( 'hours', 'lifterlms' ),
		'minute' => __( 'minutes', 'lifterlms' ),
		'second' => __( 'seconds', 'lifterlms' ),
	);
	$diffs         = array();
	foreach ( $intervals as $interval ) {
		// Create temp time from time1 and interval.
		$ttime = strtotime( '+1 ' . $interval, $time1 );
		// Set initial values.
		$add    = 1;
		$looped = 0;
		// Loop until temp time is smaller than time2.
		while ( $time2 >= $ttime ) {
			// Create new temp time from time1 and interval.
			$add++;
			$ttime = strtotime( '+' . $add . ' ' . $interval, $time1 );
			$looped++;
		}
		$time1              = strtotime( '+' . $looped . ' ' . $interval, $time1 );
		$diffs[ $interval ] = $looped;
	}
	$count = 0;
	$times = array();
	foreach ( $diffs as $interval => $value ) {
		// Break if we have needed precision.
		if ( $count >= $precision ) {
			break;
		}
		// Add value and interval if value is bigger than 0.
		if ( $value > 0 ) {
			if ( 1 != $value ) {
				$text = $l18n_plural[ $interval ];
			} else {
				$text = $l18n_singular[ $interval ];
			}
			// Add value and interval to times array.
			$times[] = $value . ' ' . $text;
			$count++;
		}
	}
	// Return string with times.
	return implode( ', ', $times );


Top ↑

Changelog Changelog

Changelog
Version Description
3.24.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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