Jump to content

determining total of days, hours and seconds between two dates


bcamp1973

Recommended Posts

thanks for the help! i've used one of the functions from the link tippy_102 provided to created the following. however, it's not working. page doesn't even load :P  My ISP is using 4.1.21-standard...not sure if that has anything to do with it?  It's not liking the last if() statement where the function calls itself... Any suggestions?

 

function age($startDate,$endDate=NULL){

$difference=strtotime($endDate ? $endDate : gmdate('Y-m-d g:i:s'))-strtotime($startDate);
$periods=array('second','minute','hour','day','week','month','year');
$lengths=array(1,60,3600,86400,604800,2630880,31570560);

for($val=sizeof($lengths)-1; $val>=0 && ($number=$difference/$lengths[$val])<=1; $val--);

if($val<0) $val=0;

$new_time=strtotime($endDate)-($difference % $lengths[$val]);
$number=floor($number);

if($number!=1) $periods[$val].='s';

$text=sprintf('%d %s ',$number,$periods[$val]); 

if(($val>=1) && ((strtotime($endDate)-$new_time)>0)){
	$text.=age($new_time);
}

return $text;
}

[code]actually, found a shorter solution...

function age($startDate,$endDate=NULL){

 

$text='';

 

$seconds=strtotime($endDate ? $endDate : gmdate('Y-m-d g:i:s'))-strtotime($startDate);

 

$periods=array('year','month','week','day','hour','minute','second');

$lengths=array(31570560,2630880,604800,86400,3600,60,1);

 

for($x=0; $x<count($periods); $x++){

if(($temp=$seconds/$lengths[$x])>=1){

$text.=floor($temp).' '.$periods[$x].($temp>=2 ? 's': '').' ';

$seconds=$seconds % $lengths[$x];

}

}

 

return $text.'old';

}[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.