Monkuar Posted December 20, 2011 Share Posted December 20, 2011 Ok I found the culprit and it's easy! function timeAgo2($timestamp, $granularity=3, $format='d:H:i:s'){ $difference = $timestamp; if($difference < 0) return '0 seconds ago'; elseif($difference < 864000){ $periods = array('week' => 604800,'day' => 86400,'hours' => 3600,'min' => 60,'seconds' => 1); $output = ''; foreach($periods as $key => $value){ if($difference >= $value){ $time = round($difference / $value); $difference %= $value; $output .= ($output ? ' ' : '').$time.' '; $output .= (($time > 1 && $key == 'day') ? $key.'s' : $key); $granularity--; } if($granularity == 0) break; } return ($output ? $output : '0 seconds').''; } else return date($format, $timestamp); } Now I will use: timeAgo2(136622,5) Which works and it will output: 2 days 14 hours 57 min 2 seconds But, if I add another "1" to it like this: timeAgo2(1136622,5) It shows: 14:03:43:42 Which is not right, it needs to show, 14 Days, 3hours 43minutes and 42 seconds.... Thank you, I finally explained myself better here, hope you understand Quote Link to comment https://forums.phpfreaks.com/topic/253527-explained-myself-alot-better-on-this-topic/ Share on other sites More sharing options...
MasterACE14 Posted December 20, 2011 Share Posted December 20, 2011 timeAgo2(1136622,5) It shows: 14:03:43:42 Which is not right, it needs to show, 14 Days, 3hours 43minutes and 42 seconds.... Thank you, I finally explained myself better here, hope you understand You do realise it is showing what you want, right? EDIT: strike that, I see what you mean... EDIT: With your 'if' statement, with that second example you're showing, $difference is greater than 864000, so that 'if' statement never executes for... timeAgo2(1136622,5) Quote Link to comment https://forums.phpfreaks.com/topic/253527-explained-myself-alot-better-on-this-topic/#findComment-1299625 Share on other sites More sharing options...
kicken Posted December 20, 2011 Share Posted December 20, 2011 elseif($difference < 864000){ 1136622 is > than 864000, so it skips your code and goes down to the date() call with the format string. The problem here is that your number is not a timestamp, it's a duration so your formatted date is not going to be what you want. You need to either remove your limit on the duration, or rework your function to accept a timestamp value so that your date() call returns the correct result. Quote Link to comment https://forums.phpfreaks.com/topic/253527-explained-myself-alot-better-on-this-topic/#findComment-1299626 Share on other sites More sharing options...
Monkuar Posted December 20, 2011 Author Share Posted December 20, 2011 elseif($difference < 864000){ 1136622 is > than 864000, so it skips your code and goes down to the date() call with the format string. The problem here is that your number is not a timestamp, it's a duration so your formatted date is not going to be what you want. You need to either remove your limit on the duration, or rework your function to accept a timestamp value so that your date() call returns the correct result. Wow, could not believe I did not see that, Thank you "MasterACE14" for your reply also. this Code works fine for what I need, I am displaying a "time left" for a user who got suspended, so if it's: timeAgo2(19236622,5) it will show the user: 32 week 6 days 16 hours 30 min 22 seconds But one problem, I need to add months to this function, would I just simply add month into that array with the corresponding seconds? Quote Link to comment https://forums.phpfreaks.com/topic/253527-explained-myself-alot-better-on-this-topic/#findComment-1299627 Share on other sites More sharing options...
SergeiSS Posted December 20, 2011 Share Posted December 20, 2011 It seems that you try to create a function that was created already exists in PHP. Did you hear about class DateTime http://ru2.php.net/manual/en/book.datetime.php ? It has many abilities. Quote Link to comment https://forums.phpfreaks.com/topic/253527-explained-myself-alot-better-on-this-topic/#findComment-1299628 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.