Hi all looking for a function that will take a unix timestamp and make a relative conversion to now. This is for comments and I was wondering if there is anything out there to convert to a timestamp like digg has, for example '3 minutes ago' or 'last month', rounding off to the nearest thing that makes sense. Anyone seen anything like this?
I wrote this some time ago. function calc_date_diff($timestamp_past = false, $timestamp_future = false, $years = true, $months = true, $days = true, $hours = true, $mins = true, $secs = true) { // Use current time if parameter is FALSE. if (!$timestamp_past) $timestamp_past = time(); if (!$timestamp_future) $timestamp_future = time(); // Attempt to convert strings to timestamp if necessary. $timestamp_future = is_string($timestamp_future) ? strtotime($timestamp_future) : intval($timestamp_future); $timestamp_past = is_string($timestamp_past) ? strtotime($timestamp_past) : intval($timestamp_past); $diff = $timestamp_future - $timestamp_past; $calc_times = array(); $timeleft = array(); // Prepare array, depending on the output we want to get. if ($years) $calc_times[] = array('Year', 'Years', 31104000); if ($months) $calc_times[] = array('Month', 'Months', 2592000); if ($days) $calc_times[] = array('Day', 'Days', 86400); if ($hours) $calc_times[] = array('Hour', 'Hours', 3600); if ($mins) $calc_times[] = array('Minute', 'Minutes', 60); if ($secs) $calc_times[] = array('Second', 'Seconds', 1); foreach ($calc_times AS $timedata) { list($time_sing, $time_plur, $offset) = $timedata; if ($diff >= $offset) { $left = floor($diff / $offset); $diff -= ($left * $offset); $timeleft[] = "{$left} ". ($left == 1 ? $time_sing : $time_plur); } } return $timeleft ? ($timestamp_future > $timestamp_past ? null : '-') . implode(', ', $timeleft) : 0; } PHP: