will35010 Posted August 9, 2010 Share Posted August 9, 2010 I have this function that calculates time between two timestamps. I want it to display days and subtract that time from the hours. Example output would be 2 Day(s) & 3 Hour(s) & 43 Minutes Right now it displays the days, but it displays it like 7.33333. I don't want anything past the decimal point and it also doesn't take away from the hour count. //function to calculate time duration using timestamps function getTIME($time) { //start/end dates $startdate = $time; $enddate = time(); //difference between the two in seconds $time_period = ( $enddate - $startdate ); $minutes=floor($time_period/60); $hours=floor($minutes/60); $days=floor($hours/24); if($hours>=24) { $daysdisplay = ($hours/24); } if($hours==1) { $hoursdisplay=$hours; } if(($minutes%60) > 1) { $minutesdisplay=($minutes%60); } else if(($minutes%60)==1) { $minutesdisplay=($minutes%60); } else { $minutesdisplay=""; } $display=$daysdisplay."Day(s)& ".$hours."Hour(s) & ".$minutesdisplay." Mins"; return $display; } Quote Link to comment https://forums.phpfreaks.com/topic/210223-function-help/ Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 You're looking for this: http://ca.php.net/manual/en/function.round.php Quote Link to comment https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097050 Share on other sites More sharing options...
freeloader Posted August 9, 2010 Share Posted August 9, 2010 Doesn't really seem like a very efficient 'time ago' script. You can find a few alternatives on this page: http://php.net/manual/en/function.time.php Search for 'ago' This one seems to be pretty good though: http://aidanlister.com/2004/04/making-time-periods-readable/ This would be a proper way of using it: function getTIME($time) { $convert = time( ) - $time; return time_duration($convert); } You could also tweak your own script to fit for this, but I wouldn't really recommend it because it's rudementary. You'd need to round ( ) them to the nearest integer. Quote Link to comment https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097051 Share on other sites More sharing options...
will35010 Posted August 9, 2010 Author Share Posted August 9, 2010 You're looking for this: http://ca.php.net/manual/en/function.round.php Fixed that. Thanks! updated code //function to calculate time duration using timestamps function getTIME($time) { //start/end dates $startdate = $time; $enddate = time(); //difference between the two in seconds $time_period = ( $enddate - $startdate ); $minutes=floor($time_period/60); $hours=floor($minutes/60); $days=floor($hours/24); if($hours>=24) { $daysdisplay = round($hours/24); } if($hours==1) { $hoursdisplay=$hours; } if(($minutes%60) > 1) { $minutesdisplay=($minutes%60); } else if(($minutes%60)==1) { $minutesdisplay=($minutes%60); } else { $minutesdisplay=""; } $display=$daysdisplay."Day(s)& ".$hours."Hour(s) & ".$minutesdisplay." Mins"; return $display; } Quote Link to comment https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097052 Share on other sites More sharing options...
will35010 Posted August 9, 2010 Author Share Posted August 9, 2010 Doesn't really seem like a very efficient 'time ago' script. You can find a few alternatives on this page: http://php.net/manual/en/function.time.php Search for 'ago' This one seems to be pretty good though: http://aidanlister.com/2004/04/making-time-periods-readable/ This would be a proper way of using it: function getTIME($time) { $convert = time( ) - $time; return time_duration($convert); } You could also tweak your own script to fit for this, but I wouldn't really recommend it because it's rudementary. You'd need to round ( ) them to the nearest integer. My script already does that. Quote Link to comment https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097053 Share on other sites More sharing options...
will35010 Posted August 9, 2010 Author Share Posted August 9, 2010 Does anybody know how to modify my script? I'm not looking to replace my script. I want to change my script is all. Quote Link to comment https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097063 Share on other sites More sharing options...
freeloader Posted August 9, 2010 Share Posted August 9, 2010 Don't double post. Just copy a pre-fixed script. I'm not going to waste my time if there are plenty of scripts out there that fit your needs. Quote Link to comment https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097083 Share on other sites More sharing options...
will35010 Posted August 9, 2010 Author Share Posted August 9, 2010 Don't double post. Just copy a pre-fixed script. I'm not going to waste my time if there are plenty of scripts out there that fit your needs. I didn't double post. Don't waste your time since you can't help anyway. If I wanted to replace it, I would have just ran to Google and copied someone's code. I wanted to fix my code so I could learn from it. Forgive me for trying to actually learn vs. copying someone's code that I didn't write. Quote Link to comment https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097105 Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 Is round() not working for you? It should have fixed your 7.3333 issue. Quote Link to comment https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097110 Share on other sites More sharing options...
will35010 Posted August 9, 2010 Author Share Posted August 9, 2010 Is round() not working for you? It should have fixed your 7.3333 issue. Yes. Round fixed the decimal point issue. But the issue that still remains is displaying in the format of my original post. Quote Link to comment https://forums.phpfreaks.com/topic/210223-function-help/#findComment-1097112 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.