Monkuar Posted August 30, 2012 Share Posted August 30, 2012 $timestamp = time(); $date_end = strtotime("+2 days",$timestamp); Okay so I do +2days right? Then in database it shows: date_end -> 1346472794 2 days 24 hours 60 min 58 seconds Why in the hell, does it add 24hours + 60minutes + 1minute? It's supposed to be for 2 DAYS, so the remaining time should be 1day 24hours 59 minutes 59 seconds, why the hell does it add that extra time? It's so annoying. It does the same crap let's say if I add +90 days: 3 month 4 week 1 day 2 hours 24 min why the hell does it add a whole nother month and 1 day 2hours 24minute? Makes absolutely no sense, last time I checked 3 months was 90 days, not 3 months 4weeks 1 day 2 hours 24 minutes? This is the php code to xfer that timestamp that I make into the time remaining: function timeAgo2($timestamp, $granularity=5, $format='d:H:i:s'){ $difference = $timestamp; if($difference < 0) return '0 seconds ago'; elseif($difference < 8640000){ $periods = array('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 : '0 seconds').''; } else return date($format, $timestamp); } If anyone could please help, much appreciated... Quote Link to comment https://forums.phpfreaks.com/topic/267785-why-does-php-date-do-this-most-annoying-thing-ever/ Share on other sites More sharing options...
Pikachu2000 Posted August 30, 2012 Share Posted August 30, 2012 date() isn't doing anything wrong. Your function is. And 3 months isn't necessarily 90 days. <?php $date_end = strtotime("+2 days", time() ); echo intval( $sec / 31536000 ) === 1 ? intval( $sec / 31536000 ) . ' Year, ' : intval( $sec / 31536000 ) . ' Years, '; echo intval( ($sec % 31536000) / 86400 ) . ' Days, ' . intval( ($sec % 86400) / 3600 ) . ' Hours, ' . intval( ($sec % 3600) / 60 ) . ' Minutes, ' . intval( $sec % 60 ) . ' Seconds'; // RETURNS: 0 Years, 2 Days, 0 Hours, 0 Minutes, 0 Seconds $date_end = strtotime("+90 days", time() ); $sec = $date_end - time(); echo intval( $sec / 31536000 ) === 1 ? intval( $sec / 31536000 ) . ' Year, ' : intval( $sec / 31536000 ) . ' Years, '; echo intval( ($sec % 31536000) / 86400 ) . ' Days, ' . intval( ($sec % 86400) / 3600 ) . ' Hours, ' . intval( ($sec % 3600) / 60 ) . ' Minutes, ' . intval( $sec % 60 ) . ' Seconds'; // RETURNS: 0 Years, 90 Days, 0 Hours, 0 Minutes, 0 Seconds $date_end = strtotime("+90 days", time() ); $sec = $date_end - time(); echo intval( $sec / 31536000 ) === 1 ? intval( $sec / 31536000 ) . ' Year, ' : intval( $sec / 31536000 ) . ' Years, '; echo intval( ($sec % 31536000) / 86400 ) . ' Days, ' . intval( ($sec % 86400) / 3600 ) . ' Hours, ' . intval( ($sec % 3600) / 60 ) . ' Minutes, ' . intval( $sec % 60 ) . ' Seconds'; // RETURNS 0 Years, 92 Days, 1 Hours, 0 Minutes, 0 Seconds Quote Link to comment https://forums.phpfreaks.com/topic/267785-why-does-php-date-do-this-most-annoying-thing-ever/#findComment-1373758 Share on other sites More sharing options...
Monkuar Posted August 30, 2012 Author Share Posted August 30, 2012 date() isn't doing anything wrong. Your function is. And 3 months isn't necessarily 90 days. <?php $seconds Sec(s)<br>"; echo intval( $sec / 31536000 ) === 1 ? intval( $sec / 31536000 ) . ' Year, ' : intval( $sec / 31536000 ) . ' Years, '; echo intval( ($sec % 31536000) / 86400 ) . ' Days, ' . intval( ($sec % 86400) / 3600 ) . ' Hours, ' . intval( ($sec % 3600) / 60 ) . ' Minutes, ' . intval( $sec % 60 ) . ' Seconds'; // RETURNS: 0 Years, 2 Days, 0 Hours, 0 Minutes, 0 Seconds $date_end = strtotime("+90 days", time() ); $sec = $date_end - time(); echo intval( $sec / 31536000 ) === 1 ? intval( $sec / 31536000 ) . ' Year, ' : intval( $sec / 31536000 ) . ' Years, '; echo intval( ($sec % 31536000) / 86400 ) . ' Days, ' . intval( ($sec % 86400) / 3600 ) . ' Hours, ' . intval( ($sec % 3600) / 60 ) . ' Minutes, ' . intval( $sec % 60 ) . ' Seconds'; // RETURNS: 0 Years, 90 Days, 0 Hours, 0 Minutes, 0 Seconds $date_end = strtotime("+90 days", time() ); $sec = $date_end - time(); echo intval( $sec / 31536000 ) === 1 ? intval( $sec / 31536000 ) . ' Year, ' : intval( $sec / 31536000 ) . ' Years, '; echo intval( ($sec % 31536000) / 86400 ) . ' Days, ' . intval( ($sec % 86400) / 3600 ) . ' Hours, ' . intval( ($sec % 3600) / 60 ) . ' Minutes, ' . intval( $sec % 60 ) . ' Seconds'; // RETURNS 0 Years, 92 Days, 1 Hours, 0 Minutes, 0 Seconds solved, im not going to us that stupid ass function, im just going to use your simple echo intval stuff, works fine. thanks pikachu. Quote Link to comment https://forums.phpfreaks.com/topic/267785-why-does-php-date-do-this-most-annoying-thing-ever/#findComment-1373760 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.