Skip to content

Instantly share code, notes, and snippets.

@mainIine
Last active December 17, 2023 13:17
Show Gist options
  • Save mainIine/1472f8d31b6eea62176683cd8b50323b to your computer and use it in GitHub Desktop.
Save mainIine/1472f8d31b6eea62176683cd8b50323b to your computer and use it in GitHub Desktop.
Get human-readable time difference between 2 dates [German]
<?php
/**
* Get human-readable time difference between 2 dates
*
* 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
*
* Original from: http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/
*
* Usage:
* $t = '2013-12-29T00:43:11+00:00';
* $t2 = '2013-11-24 19:53:04 +0100';
*
* var_dump( $this->GetTimeBetweenInHuman( $t, $t2, 1 ) ); // string '1 month' (length=7)
* var_dump( $this->GetTimeBetweenInHuman( $t, $t2, 2 ) ); // string '1 month, 4 days' (length=15)
* var_dump( $this->GetTimeBetweenInHuman( $t, $t2, 3 ) ); // string '1 month, 4 days, 5 hours' (length=24)
*
* @param mixed $time1 a time (string or timestamp)
* @param mixed $time2 a time (string or timestamp)
* @param integer $precision Optional precision
* @return string time difference
*/
function GetTimeBetweenInHuman(mixed $time1, mixed $time2, int $precision = 2 ): string
{
// If not numeric then convert timestamps
if( !is_int( $time1 ) )
{
$time1 = strtotime( $time1 );
}
if( !is_int( $time2 ) )
{
$time2 = strtotime( $time2 );
}
$_replaces = [
'year' => 'Jahr',
'month' => 'Monat',
'day' => 'Tag',
'hour' => 'Stunde',
'minute' => 'Minute',
'second' => 'Sekunde',
];
// If time1 > time2 then swap the 2 values
if( $time1 > $time2 )
{
list( $time1, $time2 ) = [$time2, $time1];
}
// Set up intervals and diffs arrays
$intervals = ['year', 'month', 'day', 'hour', 'minute', 'second'] ;
$diffs = [];
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 = [];
foreach( $diffs as $interval => $value )
{
// Break if we have needed precission
if( $count >= $precision ) {
break;
}
$interval = str_replace(array_keys($_replaces), array_values($_replaces), $interval);
// Add value and interval if value is bigger than 0
if( $value > 0 )
{
if( $value != 1 )
{
if($interval == 'Jahr' || $interval == 'Monat' || $interval == 'Tag')
{
$interval .= "e";
}
else {
$interval .= "n";
}
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}
// Return string with times
return implode( ", ", $times );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment