Monkuar Posted December 19, 2011 Share Posted December 19, 2011 function timeAgo2($timestamp, $granularity=3, $format='d:H:i:s'){ $difference = $timestamp; if($difference < 0) return '0 seconds ago'; elseif($difference < 31570560){ $periods = array('year' => 31570560, 'month' => 2630880, '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 : '').''; } else return date($format, $timestamp); } Okay, I have this: $save['removal'] = time() + (1209600); I am adding 2weeks + time() to my $save['removal'] variable. Then I will wrap my function timeAgo2 around my $save['removal'], and it shows: "19:15:50:20" but if I do $save['removal'] = time() + (300); Then it works, and shows "5minutes ago/etc" Why doesn't it work when I try to add 2weeks to my $save['removal'] variable? It seems it reads my $format if I try to set my time higher then 5minutes.... no idea...? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 19, 2011 Share Posted December 19, 2011 What is the value echoed by $save['removal']? I'll bet it makes this evaluate to FALSE: elseif($difference < 31570560){ Quote Link to comment Share on other sites More sharing options...
Monkuar Posted December 19, 2011 Author Share Posted December 19, 2011 What is the value echoed by $save['removal']? I'll bet it makes this evaluate to FALSE: elseif($difference < 31570560){ if I do: $save['removal'] = time() + (3000); echo $save['removal']; exit; It is 1324314139 When I pull it from the db it is: "1355847038" Quote Link to comment Share on other sites More sharing options...
Monkuar Posted December 19, 2011 Author Share Posted December 19, 2011 Ok, scratch that code, I was not explaining myself better. Here is the problem. I use: $save['removal'] = time() + (3600); Then when I try to echo my $save['removal'] out from my db, using this function: function timeAgo($tm,$rcs = 1) { $cur_tm = time(); $dif = $cur_tm-$tm; $pds = array('second','minute','hour','day','week','month','year','decade'); $lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600); for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]); $no = floor($no); if($no <> 1) $pds[$v] .='s'; $x=sprintf("%d %s ",$no,$pds[$v]); if(($rcs > 0)&&($v >= 1)&&(($cur_tm-$_tm) > 0)) $x .= $this->timeAgo($_tm, --$rcs); return $x; } I use this to echo it out: timeAgo($r['removal'],2); Now this is the problem.. it shows: instead of regular "XXX Minutes/seconds ago" Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 19, 2011 Share Posted December 19, 2011 Well, you say that it is working corectly when you use $save['removal'] = time() + (300); But I find that hard to believe. because of this line elseif($difference < 31570560) The current timestamp (for me) is 1324312172 which is greater than that hard-coded value in that condition. So ALL results are defaulting the the else statement of else return date($format, $timestamp); What is the purpose of '31570560'? EDIT: Als,o you are passing a timestamp to the function. Then you are using that value as $difference. A difference requires two values to be calculated. It doesn't make sense why the timestamp would be a difference, unless you were calculating the difference from January 1 1970 (which is what the timestamp is) Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted December 19, 2011 Share Posted December 19, 2011 Exactly. Just the value of time() will exceed that value, and unless the date being compared is in 1970, the elseif() will return FALSE. Why don't you explain what you're trying to accomplish instead of what you're trying to get the code to do. Quote Link to comment Share on other sites More sharing options...
Monkuar Posted December 19, 2011 Author Share Posted December 19, 2011 Exactly. Just the value of time() will exceed that value, and unless the date being compared is in 1970, the elseif() will return FALSE. Why don't you explain what you're trying to accomplish instead of what you're trying to get the code to do. Sorry, for not explaining so much, let me try again When a user on my forum system get's a Warn, I want them to wait atleast 2weeks before they can click on my "request warn removal" Link. so I am pretty much just trying to add 2 weeks to the unixtimestamp, and echo it out as "14 Days Left" right after they get warned, then the next day, It will show "13 Days left"/etc, that's is what I am trying to get it show in my "$save['removal']" value, which is showing the above error, I posted. I used the 31570560 cuz that's how many seconds are in a year, thought I needed that on that array. I scratched that old code, see my post above :'( Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 19, 2011 Share Posted December 19, 2011 OK, when a user gets a warning you should update the database to time() + 2 weeks. Then when you want to calculate the time left you should use the database value - time(). You could just update your function to do that calculation for you. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 19, 2011 Share Posted December 19, 2011 function timeUntil($endTime, $granularity=3, $format='d:H:i:s') { $difference = $endTime - time(); if($difference < 0) return '0 seconds ago'; elseif($difference < 200630560) { $periods = array('year' => 31570560, 'month' => 2630880, '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 : '').''; } else return date($format, $timestamp); } echo timeUntil(time()+9); // 9 seconds echo timeUntil(time()+99); // 2 min 39 seconds echo timeUntil(time()+9999); // 3 hours 47 min 39 seconds echo timeUntil(time()+99999); // 1 day 4 hours 47 min echo timeUntil(time()+999999); // 2 week 5 days 14 hours echo timeUntil(time()+9999999); // 4 month 3 week 3 days echo timeUntil(time()+99999999); // 3 year 2 month 7 hours Quote Link to comment Share on other sites More sharing options...
Monkuar Posted December 20, 2011 Author Share Posted December 20, 2011 function timeUntil($endTime, $granularity=3, $format='d:H:i:s') { $difference = $endTime - time(); if($difference < 0) return '0 seconds ago'; elseif($difference < 200630560) { $periods = array('year' => 31570560, 'month' => 2630880, '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 : '').''; } else return date($format, $timestamp); } echo timeUntil(time()+9); // 9 seconds echo timeUntil(time()+99); // 2 min 39 seconds echo timeUntil(time()+9999); // 3 hours 47 min 39 seconds echo timeUntil(time()+99999); // 1 day 4 hours 47 min echo timeUntil(time()+999999); // 2 week 5 days 14 hours echo timeUntil(time()+9999999); // 4 month 3 week 3 days echo timeUntil(time()+99999999); // 3 year 2 month 7 hours Hey, that works, but when they refresh it will just keep showing "9seconds" instead of 8seconds, 7seconds,6seconds/etc. :confused: Quote Link to comment 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.