Calculate the time that has passed since a specified Unix formatted date and time, and show it in a human readable format, all in a PHP function.
function TimeSince($original) // $original should be the original date and time in Unix format
{
// Common time periods as an array of arrays
$periods = array(
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
array(60 * 60 , 'hour'),
array(60 , 'minute'),
);
$today = time();
$since = $today - $original; // Find the difference of time between now and the past
// Loop around the periods, starting with the biggest
for ($i = 0, $j = count($periods); $i < $j; $i++)
{
$seconds = $periods[$i][0];
$name = $periods[$i][1];
// Find the biggest whole period
if (($count = floor($since / $seconds)) != 0)
{
break;
}
}
$output = ($count == 1) ? '1 '.$name : "$count {$name}s";
if ($i + 1 < $j)
{
// Retrieving the second relevant period
$seconds2 = $periods[$i + 1][0];
$name2 = $periods[$i + 1][1];
// Only show it if it's greater than 0
if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0)
{
$output .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
}
}
return $output;
}
echo TimeSince(1249091056);
The provided code is a PHP function named TimeSince
designed to calculate the time elapsed since a given date and time, specified in Unix format (Unix timestamp). It returns a human-readable string representing this time difference. Here’s an explanation:
- Function Definition:
function TimeSince($original)
takes one argument,$original
, which is the date and time in Unix timestamp format. - Time Periods Array: The
$periods
array defines various time periods in seconds (years, months, weeks, days, hours, minutes) along with their textual representation. - Current Time Calculation:
$today = time();
gets the current time in Unix timestamp format.$since = $today - $original;
calculates the difference in seconds between the current time and the provided timestamp. - Calculating the Largest Time Period: The
for
loop iterates through the$periods
array, starting from the largest time unit (year). It calculates how many of each unit fits into the$since
time difference. Once it finds a time unit that fits (floor($since / $seconds) != 0
), it breaks out of the loop. - Formatting the Output:
$output
is formatted to show the elapsed time in the largest fitting time unit found. If the count is 1, it uses the singular form of the unit; otherwise, it uses the plural. - Adding a Second Time Period: The code then checks if there’s a smaller time unit that can also be used to describe the remaining time. If so, it appends this information to the
$output
. - Returning the Result: The function returns the formatted string
$output
, which describes the time elapsed since the original timestamp in a human-readable format. - Example Usage:
echo TimeSince(1249091056);
This line would output the time elapsed since the Unix timestamp1249091056
.