DirtySnipe Posted May 27, 2009 Share Posted May 27, 2009 Say I have two veriables $a and $b They are both type datetime format $a is a start time and $b is a finish time. Can anyone tell me how i can get a percentage 0-100 of completion? Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/ Share on other sites More sharing options...
Daniel0 Posted May 27, 2009 Share Posted May 27, 2009 That doesn't make any sense. If you have 2009-05-27 and 2009-05-30 and you are "30% completed", what would that mean, and what data would you use to get to that number? Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-843260 Share on other sites More sharing options...
Maq Posted May 27, 2009 Share Posted May 27, 2009 If I understand correctly and if you know where they currently are in the date range, you could use logic similar to: a = start b = end c = current percent = (c - a) / (b - a) * 100 Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-843278 Share on other sites More sharing options...
DirtySnipe Posted May 28, 2009 Author Share Posted May 28, 2009 thank you. now I just gotta figure out how to put that into php...lol Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-843915 Share on other sites More sharing options...
DirtySnipe Posted May 28, 2009 Author Share Posted May 28, 2009 Im a complete nubin when it comes to php. Can some one point me as to what im doing wrong (probably alot..lol) $percent =(echo date('Y-m-d H:i:s') - $beginProductionTime)/($endProductionTime - echo date('Y-m-d H:i:s')*100); Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-843926 Share on other sites More sharing options...
Maq Posted May 28, 2009 Share Posted May 28, 2009 You can't do it like that. What kind of precision do you want? Days, hours, minutes? Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844136 Share on other sites More sharing options...
DirtySnipe Posted May 28, 2009 Author Share Posted May 28, 2009 minutes would probably be the best. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844282 Share on other sites More sharing options...
roopurt18 Posted May 29, 2009 Share Posted May 29, 2009 Untested but should get you close... <?php // three inputs $start = '2009-01-01'; $end = '2008-01-01 12:00:48'; $percentage = .3; // order them correctly $start = strtotime( $start ); $end = strtotime( $end ); if( !$start || !$end ) { throw new Exception( 'Invalid dates' ); } $min = min( $start, $end ); $max = max( $start, $end ); // $min is the begin date in seconds, $max is the end date in seconds echo date( 'Y-m-d H:i:s', $min + ($percentage * ($max - $min)) ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844640 Share on other sites More sharing options...
DirtySnipe Posted May 29, 2009 Author Share Posted May 29, 2009 I'd like to say thank you all for your time so far and you are all brilliant. roopurt18 I tried that code but not sure what is wrong. I changed the date to the following $start = '2009-05-29'; $end = '2009-06-01 12:00:48'; The output displays 2009-05-30 01:12:14 and not a % ?? Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844748 Share on other sites More sharing options...
roopurt18 Posted May 29, 2009 Share Posted May 29, 2009 My code takes two dates, calculates the number of seconds between them, multiplies by a percentage, and then adds that percentage to the first date. Finally it outputs the result as a date. Otherwise what you are asking for makes no sense mathematically so you'll need to be more clear. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844760 Share on other sites More sharing options...
Daniel0 Posted May 29, 2009 Share Posted May 29, 2009 I think what he means that if start is 2009-05-28, current is 2009-05-29 and end is 2009-05-30 then the result should be 50%. I.e. something like this: <?php $start = '2009-05-28'; $end = '2009-05-30'; $start = strtotime($start); $end = strtotime($end); if (!$end || !$start) { throw new Exception('Invalid dates.'); } else if ($start > $end) { throw new Exception('Start date is larger than end date.'); } $diff = $end - $start; $current = time(); $cdiff = $current - $start; if ($cdiff > $diff) { $percentage = 1.0; } else if ($current < $start) { $percentage = 0.0; } else { $percentage = $cdiff / $diff; } printf('%.2f%%', $percentage * 100); Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844766 Share on other sites More sharing options...
DirtySnipe Posted May 29, 2009 Author Share Posted May 29, 2009 OK I have the following code:- $start = '2009-05-29 12:00:48'; $end = '2009-06-01 12:00:48'; $start = strtotime($start); $end = strtotime($end); if (!$end || !$start) { throw new Exception('Invalid dates.'); } else if ($start > $end) { throw new Exception('Start date is larger than end date.'); } $diff = $end - $start; $current = time(); $cdiff = $current - $start; if ($cdiff > $diff) { $percentage = 1.0; } else if ($current < $start) { $percentage = 0.0; } else { $percentage = $cdiff / $diff; } and i have placed the following in the table to show the results. printf('%.2f%%', $percentage * 100); But i get the following output 0.12%5 Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844837 Share on other sites More sharing options...
Daniel0 Posted May 29, 2009 Share Posted May 29, 2009 Well, those two date inputs are all in the past. You need to explain what you are trying to do. Otherwise we cannot really help you. We have all been trying to guess what you are trying to do, but all the times you simply just come back and say you aren't getting the correct results. That's like walking in saying that you're trying to get the number 5. I can provide you millions of ways you can get the number 5, but unless you tell me how you plan on getting to that number I can't give you the solution you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844839 Share on other sites More sharing options...
DirtySnipe Posted May 29, 2009 Author Share Posted May 29, 2009 sorry i missed part of the code. Please look up at editing post. What im trying to do is take a start date and a finish date. And work out a percentage to completion. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844841 Share on other sites More sharing options...
DirtySnipe Posted May 29, 2009 Author Share Posted May 29, 2009 also as a note both dates are not in the past. The start date is but the end date is in june. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844843 Share on other sites More sharing options...
Daniel0 Posted May 29, 2009 Share Posted May 29, 2009 What did you expect the result to be? also as a note both dates are not in the past. The start date is but the end date is in june. It wasn't before your edit. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844844 Share on other sites More sharing options...
DirtySnipe Posted May 29, 2009 Author Share Posted May 29, 2009 The code looks like its working but it is outputting a %5 after the number. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844845 Share on other sites More sharing options...
Daniel0 Posted May 29, 2009 Share Posted May 29, 2009 That's something you're outputting somewhere else. Right now <?php $start = '2009-05-29 12:00:48'; $end = '2009-06-01 12:00:48'; $start = strtotime($start); $end = strtotime($end); if (!$end || !$start) { throw new Exception('Invalid dates.'); } else if ($start > $end) { throw new Exception('Start date is larger than end date.'); } $diff = $end - $start; $current = time(); $cdiff = $current - $start; if ($cdiff > $diff) { $percentage = 1.0; } else if ($current < $start) { $percentage = 0.0; } else { $percentage = $cdiff / $diff; } printf('%.2f%%', $percentage * 100); outputs 1.88%, but that will of course change. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844849 Share on other sites More sharing options...
DirtySnipe Posted May 29, 2009 Author Share Posted May 29, 2009 hmm nope. I wonder if this problem i have is anything to do with me outputting the table results into a table?? all of my code so far. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844851 Share on other sites More sharing options...
Daniel0 Posted May 29, 2009 Share Posted May 29, 2009 Uh... yeah it does. You cannot do echo printf. printf already does output it and it returns the length of the outputted string, which is what you are then echoing. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844852 Share on other sites More sharing options...
DirtySnipe Posted May 29, 2009 Author Share Posted May 29, 2009 ahhh bliss Thank you soo much. You have been a great help. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844854 Share on other sites More sharing options...
.josh Posted May 29, 2009 Share Posted May 29, 2009 If I understand correctly and if you know where they currently are in the date range, you could use logic similar to: a = start b = end c = current percent = (c - a) / (b - a) * 100 this is what you want. All you have to do is convert your vars into timestamps and insert where appropriate. edit: oops my bad. I didn't realize there was another page to this thead. Quote Link to comment https://forums.phpfreaks.com/topic/159887-solved-how-to-calculate-two-dates-into-a-percentage/#findComment-844907 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.