dozent_datetime_diff( int|string $datetime1, null $datetime2 = null, string $display_mode = 'full' )

Generate Precious Human Readable Time Difference


Description

Example usage:

dozent_datetime_diff( $time_start, $time_end );

Parameters

$datetime1

(int|string) (Required) Start Time

$datetime2

(null) (Optional) Time End

Default value: null

$display_mode

(string) (Optional) full | clock

Default value: 'full'


Return

(string)


Source

File: includes/core-functions.php

	function dozent_datetime_diff( $datetime1, $datetime2 = null, $display_mode = 'full' ) {

		$datetimeObj1 = new DateTime( $datetime1 );
		$dateTime     = $datetimeObj1->diff( new DateTime( $datetime2 ) );

		$datetimeArray = [];

		if ( $dateTime->y ) {
			$datetimeArray['years'] = str_pad( $dateTime->y, 2, 0, STR_PAD_LEFT );
		}
		if ( $dateTime->m ) {
			$datetimeArray['months'] = str_pad( $dateTime->m, 2, 0, STR_PAD_LEFT );
		}
		if ( $dateTime->d ) {
			$datetimeArray['days'] = str_pad( $dateTime->d, 2, 0, STR_PAD_LEFT );
		}
		$datetimeArray['hours']   = str_pad( $dateTime->h, 2, 0, STR_PAD_LEFT );
		$datetimeArray['minutes'] = str_pad( $dateTime->i, 2, 0, STR_PAD_LEFT );
		$datetimeArray['seconds'] = str_pad( $dateTime->s, 2, 0, STR_PAD_LEFT );

		$datetimeArray = apply_filters( 'dozent_datetime_diff_array', $datetimeArray, $datetime1, $datetime2 );


		$format           = "";
		$total_properties = dozent_count( $datetimeArray );
		$i                = 0;
		foreach ( $datetimeArray as $property => $time_value ) {
			$i ++;

			if ( $display_mode === 'clock' ) {

				if ( in_array( $property, [ 'hours', 'minutes', 'seconds' ] ) ) {
					$format .= $time_value;
					if ( $i < $total_properties ) {
						$format .= ":";
					}
				}

			} else {
				$time_name = dozent_get_datetime_property_names( $property );

				if ( $time_name ) {
					$format .= "{$time_value} {$time_name}";
					if ( $i < $total_properties ) {
						$format .= ", ";
					}
				}
			}


		}

		return apply_filters( 'dozent_datetime_diff', $format, $datetimeArray, $datetime1, $datetime2, $format );
	}


Changelog

Changelog
Version Description
DozentLMS 1.0.0 Introduced.