rofl90 Posted June 27, 2008 Share Posted June 27, 2008 Quite strange, anybody know whats wrong with it? public static function since($original) { $original=strtotime($original); $labels=array('year','month','week','day','hour','minute'); $time_in_seconds=array(60*60*24*365, 60*60*24*30, 60*60*24*7, 60*60*24, 60*60, 60); $last_active=time()-$original; if($last_active <= 0) { return 'Today'; } // loop through each of the time units for($i=0,$j=count($labels); $i<$j; $i++) { $seconds_remaining=floor($last_active/$time_in_seconds[$i]); // if this is the biggest chunk of the pie, break out if($seconds_remaining > 0) { // if this is not the the last element (minute) if($i < $j) { for($i=0,$j=count($labels); $i<$j; $i++) { $count2=floor(($last_active-($time_in_seconds[$i]*$seconds_remaining))/$seconds2); } } else { } } } $print=($seconds==1)?'1 '.$name:$seconds.' '.$names.' ago'; $print.=($seconds2==1)?', 1 '.$name2:', '.$seconds2.' '.$name2.'s'; return $print; } Link to comment https://forums.phpfreaks.com/topic/112232-function-to-do-with-time-is-broken/ Share on other sites More sharing options...
ag3nt42 Posted June 27, 2008 Share Posted June 27, 2008 what exactly is it doing wrong? Link to comment https://forums.phpfreaks.com/topic/112232-function-to-do-with-time-is-broken/#findComment-576195 Share on other sites More sharing options...
rofl90 Posted June 27, 2008 Author Share Posted June 27, 2008 Ok, it's totally broke, can somebody put me on my way it should take a time and then strtotime() then return eg for 3600 1 hour, 0 minutes it should always return it's nearest rounded below unit and then it's next below unit ex for 3 hours 12 minutes 6 seconds, AGO*** if you fed it that in unix time or whatever it should come out as 3 hours, 12 minutes ago eg it should measure the difference between the $original and now and return it as its nearest value below and the next value below. Link to comment https://forums.phpfreaks.com/topic/112232-function-to-do-with-time-is-broken/#findComment-576282 Share on other sites More sharing options...
rofl90 Posted June 27, 2008 Author Share Posted June 27, 2008 This is what I came up with public static function since($original) { $original = strtotime($original); $originalM = (int)($original - time()); if($originalM < (int)(60)) { return time("s \seconds \ago", $originalM); } elseif($originalM < (int)(60 * 60)) { return time("i \minutes, s \seconds \ago", $originalM); } elseif($originalM < (int)(60 * 60 * 24)) { return time("G /hours, i \minutes \ago", $originalM); } elseif($originalM < (int)(60 * 60 * 24 * 30)) { return time("j \days, G \hours \ago", $originalM); } elseif($originalM < (int)(60 * 60 * 24 * 365)) { return time("n \months, j \days ago", $originalM); } else { return time("Y \years, n \months \ago"); } } When I use it it just echos out the current timestamp. Link to comment https://forums.phpfreaks.com/topic/112232-function-to-do-with-time-is-broken/#findComment-576303 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.