Hi, I’ve found this function from http://snipplr.com/view/19353/timespan/
function timespan($time1, $time2 = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
{
// Array with the output formats
$output = preg_split('/[^a-z]+/', strtolower((string) $output));
// Invalid output
if (empty($output))
return FALSE;
// Make the output values into keys
extract(array_flip($output), EXTR_SKIP);
// Default values
$time1 = max(0, (int) $time1);
$time2 = empty($time2) ? time() : max(0, (int) $time2);
// Calculate timespan (seconds)
$timespan = abs($time1 - $time2);
// All values found using Google Calculator.
// Years and months do not match the formula exactly, due to leap years.
// Years ago, 60 * 60 * 24 * 365
isset($years) and $timespan -= 31556926 * ($years = (int) floor($timespan / 31556926));
// Months ago, 60 * 60 * 24 * 30
isset($months) and $timespan -= 2629744 * ($months = (int) floor($timespan / 2629743.83));
// Weeks ago, 60 * 60 * 24 * 7
isset($weeks) and $timespan -= 604800 * ($weeks = (int) floor($timespan / 604800));
// Days ago, 60 * 60 * 24
isset($days) and $timespan -= 86400 * ($days = (int) floor($timespan / 86400));
// Hours ago, 60 * 60
isset($hours) and $timespan -= 3600 * ($hours = (int) floor($timespan / 3600));
// Minutes ago, 60
isset($minutes) and $timespan -= 60 * ($minutes = (int) floor($timespan / 60));
// Seconds ago, 1
isset($seconds) and $seconds = $timespan;
// Remove the variables that cannot be accessed
unset($timespan, $time1, $time2);
// Deny access to these variables
$deny = array_flip(array('deny', 'key', 'difference', 'output'));
// Return the difference
$difference = array();
foreach ($output as $key)
{
if (isset($$key) AND ! isset($deny[$key]))
{
// Add requested key to the output
$difference[$key] = $$key;
}
}
// Invalid output formats string
if (empty($difference))
return FALSE;
// If only one output format was asked, don't put it in an array
if (count($difference) === 1)
return current($difference);
// Return array
return $difference;
}
and I can’t seem to get it to work, can you tell what time format you need to pass the time to the function?
Thanks
Going by the values used in the conditionals (the purple ints)
isset($years) and $timespan -= 31556926 * ($years = (int) floor($timespan / 31556926));
// Months ago, 60 * 60 * 24 * 30
isset($months) and $timespan -= 2629744 * ($months = (int) floor($timespan / 2629743.83));
// Weeks ago, 60 * 60 * 24 * 7
isset($weeks) and $timespan -= 604800 * ($weeks = (int) floor($timespan / 604800));
// Days ago, 60 * 60 * 24
isset($days) and $timespan -= 86400 * ($days = (int) floor($timespan / 86400));
// Hours ago, 60 * 60
isset($hours) and $timespan -= 3600 * ($hours = (int) floor($timespan / 3600));
// Minutes ago, 60
isset($minutes) and $timespan -= 60 * ($minutes = (int) floor($timespan / 60));
// Seconds ago, 1
I’d guess it wants a unix timestamp http://php.net/manual/en/function.time.php
Try calling the function with
$time_test = time();
timespan($time_test - 1357);
Thanks,
I’d tried time() but it just outputs an array.
echo timespan($time_test - 1357);
just says Array
and
$t = timespan($time_test - 1357);
var_dump($t);
Gives
array(7) {
[“years”]=>
int(0)
[“months”]=>
int(0)
[“weeks”]=>
int(0)
[“days”]=>
int(0)
[“hours”]=>
int(0)
[“minutes”]=>
int(22)
[“seconds”]=>
int(37)
}
Could it be a faulty function?
That’s what it should return.
Oh yeah, Thanks Alieandev, I was expecting something like “1 hour ago”, but looking again I see it wouldn’t do that.
$time1 = time();
$time2 = time() + 60;
$result = timespan($time1, $time2);
fred($result, '$result'); // my [retty output fuinction
foreach($result as $time => $portion):
echo jj;
echo $time;
echo ' --> ';
echo $portion;
endforeach;
die;
Output: http://www.graabr.com/l1cJH4/
I reckon this is hard-work in finding a solution and trying to connect it to the correct problem 
Why not give us your problem and maybe someone can give you a solution which will be ideal for your requirements.
.
I’ve extended the above function to do what I was expecting it to do to start with, thought it might be useful to others.
I’m sure there would be a more elegant way to do it but it works for me.
function timespan($time1, $time2 = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
{
$thetime = $time1;
// Array with the output formats
$output = preg_split('/[^a-z]+/', strtolower((string) $output));
// Invalid output
if (empty($output))
return FALSE;
// Make the output values into keys
extract(array_flip($output), EXTR_SKIP);
// Default values
$time1 = max(0, (int) $time1);
$time2 = empty($time2) ? time() : max(0, (int) $time2);
// Calculate timespan (seconds)
$timespan = abs($time1 - $time2);
// All values found using Google Calculator.
// Years and months do not match the formula exactly, due to leap years.
// Years ago, 60 * 60 * 24 * 365
isset($years) and $timespan -= 31556926 * ($years = (int) floor($timespan / 31556926));
// Months ago, 60 * 60 * 24 * 30
isset($months) and $timespan -= 2629744 * ($months = (int) floor($timespan / 2629743.83));
// Weeks ago, 60 * 60 * 24 * 7
isset($weeks) and $timespan -= 604800 * ($weeks = (int) floor($timespan / 604800));
// Days ago, 60 * 60 * 24
isset($days) and $timespan -= 86400 * ($days = (int) floor($timespan / 86400));
// Hours ago, 60 * 60
isset($hours) and $timespan -= 3600 * ($hours = (int) floor($timespan / 3600));
// Minutes ago, 60
isset($minutes) and $timespan -= 60 * ($minutes = (int) floor($timespan / 60));
// Seconds ago, 1
isset($seconds) and $seconds = $timespan;
// Remove the variables that cannot be accessed
unset($timespan, $time1, $time2);
// Deny access to these variables
$deny = array_flip(array('deny', 'key', 'difference', 'output'));
// Return the difference
$difference = array();
foreach ($output as $key)
{
if (isset($$key) AND ! isset($deny[$key]))
{
// Add requested key to the output
$difference[$key] = $$key;
}
}
// Invalid output formats string
if (empty($difference))
return FALSE;
// If only one output format was asked, don't put it in an array
if (count($difference) === 1)
return current($difference);
// Return array
if($difference[years] >= 1 || $difference[months] >= 1 || $difference[weeks] >= 1){
return date('H:i jS F Y',$thetime);
}
if($difference[days] == 1){
return "about ".$difference[days]." day ago";
}
if($difference[days] > 1){
return "about ".$difference[days]." days ago";
}
if($difference[hours] == 1 && $difference[minutes] > 0){
return "about ".$difference[hours]." hour ".$difference[minutes]." minutes ago";
}
if($difference[hours] > 1 && $difference[minutes] > 0){
return "about ".$difference[hours]." hours ".$difference[minutes]." minutes ago";
}
if($difference[hours] == 1){
return "about ".$difference[hours]." hour ago";
}
if($difference[hours] > 1){
return "about ".$difference[hours]." hours ago";
}
if($difference[minutes] == 1 || $difference[minutes] == 0 && $difference[seconds] > 0 ){
return "about 1 minute ago";
}
if($difference[minutes] > 1){
return "about ".$difference[minutes]." minutes ago";
}
}