bcamp1973 Posted April 6, 2007 Share Posted April 6, 2007 If i have two timestamps, is there a built in function to give me the different between the two as days, hours and seconds? Link to comment https://forums.phpfreaks.com/topic/45932-determining-total-of-days-hours-and-seconds-between-two-dates/ Share on other sites More sharing options...
kenrbnsn Posted April 6, 2007 Share Posted April 6, 2007 No, you have to write it yourself. Ken Link to comment https://forums.phpfreaks.com/topic/45932-determining-total-of-days-hours-and-seconds-between-two-dates/#findComment-223136 Share on other sites More sharing options...
bcamp1973 Posted April 6, 2007 Author Share Posted April 6, 2007 any hints on how? Link to comment https://forums.phpfreaks.com/topic/45932-determining-total-of-days-hours-and-seconds-between-two-dates/#findComment-223137 Share on other sites More sharing options...
tippy_102 Posted April 7, 2007 Share Posted April 7, 2007 The user posted comments on this page will be helpfull to you: http://ca.php.net/manual/en/function.time.php Link to comment https://forums.phpfreaks.com/topic/45932-determining-total-of-days-hours-and-seconds-between-two-dates/#findComment-223299 Share on other sites More sharing options...
Eugene Posted April 7, 2007 Share Posted April 7, 2007 $seconds = $timeStamp2 - $timeStamp2; $minutes = ($seconds / 60); $hour = (($seconds / 60) / 60); Don't hold me to this though, I just coded it in about 2 seconds. Link to comment https://forums.phpfreaks.com/topic/45932-determining-total-of-days-hours-and-seconds-between-two-dates/#findComment-223336 Share on other sites More sharing options...
bcamp1973 Posted April 7, 2007 Author Share Posted April 7, 2007 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 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; } Link to comment https://forums.phpfreaks.com/topic/45932-determining-total-of-days-hours-and-seconds-between-two-dates/#findComment-223618 Share on other sites More sharing options...
bcamp1973 Posted April 7, 2007 Author Share Posted April 7, 2007 [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] Link to comment https://forums.phpfreaks.com/topic/45932-determining-total-of-days-hours-and-seconds-between-two-dates/#findComment-223644 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.