Skip to content

Instantly share code, notes, and snippets.

@DennisRas
Created August 26, 2011 14:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DennisRas/1173514 to your computer and use it in GitHub Desktop.
Save DennisRas/1173514 to your computer and use it in GitHub Desktop.
My suggestion for a time function
<?php
if ( ! function_exists('time_ago'))
{
function time_ago($time, $max_units = NULL)
{
$lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
$units = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade');
$unit_string_array = array();
$max_units = (is_numeric($max_units) && in_array($max_units, range(1,8))) ? $max_units : sizeOf($lengths);
$diff = (is_numeric($time) ? time() - $time : time() - strtotime($time));
$future = ($diff < 0) ? 1 : 0;
$diff = abs($diff); // Let's get positive!
$total_units = 0;
for ($i = sizeOf($lengths) - 1; $i >= 0; $i--)
{
if ($diff > $lengths[$i] && $total_units < $max_units)
{
$amount = floor($diff / $lengths[$i]);
$mod = $diff % $lengths[$i];
$unit_string_array[] = $amount . ' ' . $units[$i] . (($amount == 1) ? '' : 's');
$diff = $mod;
$total_units++;
}
}
return ($future) ? implode($unit_string_array, ', ') . ' to go' : implode($unit_string_array, ', ') . ' ago';
}
}
// 3 seconds ago
echo time_ago(time() - 3) . "<br />";
// 1 minute, 30 seconds ago
echo time_ago(time() - 90, 'invalidparam') . "<br />";
// 3 minutes ago
echo time_ago(time() - 180) . "<br />";
// 1 hour, 30 minutes, 6 seconds ago
echo time_ago(time() - 3604 * 1.5) . "<br />";
// 3 hours, 9 seconds ago
echo time_ago(time() - 3603 * 3) . "<br />";
// 1 day, 1 hour, 50 seconds ago
echo time_ago(time() - 3602 * 25) . "<br />";
// 5 days (note the 2nd param)
echo time_ago(time() - 3601 * 24 * 5, 1) . "<br />";
// Depends on when you run the script :)
echo time_ago('Thursday, August 25, 23:00') . "<br />";
// 33 seconds to go
echo time_ago(time() + 343) . "<br />";
// 1 month, 2 weeks, 5 days to go (note the 2nd param)
echo time_ago(time() + 36201 * 24 * 5, 3) . "<br />";
<?php
if ( ! function_exists('time_ago'))
{
function time_ago($time, $max_units = NULL)
{
$lengths = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600);
$units = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade');
$units_plural = array('seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years', 'decades');
$unit_string_array = array();
$max_units = (is_numeric($max_units) && in_array($max_units, range(1,8))) ? $max_units : sizeOf($lengths);
$diff = (is_numeric($time) ? time() - $time : time() - strtotime($time));
$future = ($diff < 0) ? 1 : 0;
$diff = abs($diff); // Let's get positive!
$total_units = 0;
for ($i = sizeOf($lengths) - 1; $i >= 0; $i--)
{
if ($diff > $lengths[$i] && $total_units < $max_units)
{
$amount = floor($diff / $lengths[$i]);
$mod = $diff % $lengths[$i];
$unit_string_array[] = $amount . ' ' . (($amount == 1) ? $units[$i] : $units_plural[$i]);
$diff = $mod;
$total_units++;
}
}
return ($future) ? implode($unit_string_array, ', ') . ' to go' : implode($unit_string_array, ', ') . ' ago';
}
}
// 3 seconds ago
echo time_ago(time() - 3) . "<br />";
// 1 minute, 30 seconds ago
echo time_ago(time() - 90, 'invalidparam') . "<br />";
// 3 minutes ago
echo time_ago(time() - 180) . "<br />";
// 1 hour, 30 minutes, 6 seconds ago
echo time_ago(time() - 3604 * 1.5) . "<br />";
// 3 hours, 9 seconds ago
echo time_ago(time() - 3603 * 3) . "<br />";
// 1 day, 1 hour, 50 seconds ago
echo time_ago(time() - 3602 * 25) . "<br />";
// 5 days (note the 2nd param)
echo time_ago(time() - 3601 * 24 * 5, 1) . "<br />";
// Depends on when you run the script :)
echo time_ago('Thursday, August 25, 23:00') . "<br />";
// 33 seconds to go
echo time_ago(time() + 343) . "<br />";
// 1 month, 2 weeks, 5 days to go (note the 2nd param)
echo time_ago(time() + 36201 * 24 * 5, 3) . "<br />";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment