Skip to content

Instantly share code, notes, and snippets.

@danielwolf1
Forked from ozh/gist:8169202
Last active October 12, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielwolf1/cb0516fc6c180c4106c0 to your computer and use it in GitHub Desktop.
Save danielwolf1/cb0516fc6c180c4106c0 to your computer and use it in GitHub Desktop.
Human readable time difference between 2 dates in PHP. Allows to pass translations for intervals.
<?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
*
* From: http://www.if-not-true-then-false.com/2010/php-calculate-real-differences-between-two-dates-or-timestamps/
*
* @param mixed $time1 a time (string or timestamp)
* @param mixed $time2 a time (string or timestamp)
* @param integer $precision Optional precision
* @param array $intervals Optional translated intervals
* @return string time difference
*/
function get_date_diff($time1, $time2, $precision = 2, $intervals = array(
'year' => array('year', 'years'),
'month' => array('month', 'months'),
'day' => array('day', 'days'),
'hour' => array('hour', 'hours'),
'minute' => array('minute', 'minutes'),
'second' => array('second', 'seconds')
))
{
// If not numeric then convert timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($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
$diffs = array();
foreach ($intervals as $interval => $interval_label) {
// 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 precission
if ($count >= $precision) {
break;
}
// Add value and interval if value is bigger than 0
if ($value > 0) {
// Add value and interval to times array
$times[] = $value . " " . $intervals[$interval][$value == 1 ? 0 : 1];
$count++;
}
}
// Return string with times
return implode(", ", $times);
}
/**
Usage:
$t = '2013-12-29T00:43:11+00:00';
$t2 = '2013-11-24 19:53:04 +0100';
var_dump( get_date_diff( $t, $t2, 1 ) ); // string '1 month' (length=7)
var_dump( get_date_diff( $t, $t2, 2, array(
'year' => array('Jahr', 'Jahre'),
'month' => array('Monat', 'Monate'),
'day' => array('Tag', 'Tage'),
'hour' => array('Stunde', 'Stunden'),
'minute' => array('Minute', 'Minuten'),
'second' => array('Sekunde', 'Sekunden')))); // string '1 Monat, 4 Tage" 2 Monate, 20 Tage' (length=15)
*/
@congomonster
Copy link

Can't get it to work. Can you please post describe where i have to put the var_dump?

@danielwolf1
Copy link
Author

What do you mean with "Where I have to put the var_dump"? var_dumping the output is just for demo purpose. What exactly doesn't work for you?

@congomonster
Copy link

I get it to work. I have changed the intervals array and set the precision to 6.
I include this script from another php page and do a
$gesamtzeit = get_date_diff($row['punkt_001'], $row['punkt_087']);
This is my code now. Is that right?

array('Jahr', 'Jahre'), 'month' => array('Monat', 'Monate'), 'day' => array('Tag', 'Tage'), 'hour' => array('Stunde', 'Stunden'), 'minute' => array('Minute', 'Minuten'), 'second' => array('Sekunde', 'Sekunden') )) { // If not numeric then convert timestamps if (!is_int($time1)) { $time1 = strtotime($time1); } if (!is_int($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 $diffs = array(); foreach ($intervals as $interval => $interval_label) { // 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 precission if ($count >= $precision) { break; } // Add value and interval if value is bigger than 0 if ($value > 0) { // Add value and interval to times array $times[] = $value . " " . $intervals[$interval][$value == 1 ? 0 : 1]; $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