How can I calculate percentage between two time values in php:
Completed time 4min. and 35sec. Maximum time 11min.
What would the remaining 6min and 25sec be in percentage?
First, make those minutes into seconds:
4 min 35 sec = 275 seconds
11 min = 660 seconds
Your percentage of remaining time will be (275 / 660) * 100. The percentage of time left would be ((660 - 275) / 660) * 100. Of course, that's all in seconds. Don't know how you are receiving that time in php, but it might look like:
$maxTime = 660;
$timeTaken = 275;
$percentage = ($timeTaken / $maxTime) * 100;
// To get percentage of time left
$percentLeft = (($maxTime - $timeTaken) / $maxTime) * 100;
If you have minutes and second separately, it will be:
$min = 4;
$sek = 35;
$maxmin = 11;
$percentage = round((60*$maxmin - (60*$min + $sek))/($maxmin*60)*100,2);
result is 58,33
Related
I don't know if this will make sense but I would like to get the time from percentage of a value, I have the following as an input example:
mypercentage = 50;
mytime = "00:59:59"
As you can see, my time is 60 minutes (1 hour) and 50% of that is 30 minutes therefore from the 2 above inputs I would like to get the following output
(50 % of an hour):
00:30:00
Explode the string into an array
Convert the 3 elements into numbers
Find the value in seconds. $array[0]*3600+$array[1]*60+$array[2]
Calculate the time from the value in step 3 and the percentage.
Use /3600 and /60 to find the hour and minutes and %60 to find the seconds.
put the values in an array and implode to a string, or just use
$hour.":".$minute.":".$second
Thank you all who responded, I found this solution to work:
$mypercentage = 50;
$mytime = '00:59:59';
$totaltime = date('H:i:s', strtotime($mytime));
$seconds = strtotime("1970-01-01 $totaltime UTC");
$per_seconds = ($mypercentage / 100) * $seconds;
$time_from_percentage = gmdate("H:i:s", $per_seconds);
echo $time_from_percentage;
I need a Function that rounds the minutes of a time stamp to round up the next 15 minutes when the time is 5 minutes above the previous 15 minutes.
01:05:00 = 01:15:00
01:04:00 = 01:00:00
01:20:00 = 01:30:00
01:19:00 = 01:15:00
I have this right now but the flaw is some times it will round down due to the really poor attempt on my part.
function roundTime10($timestamp10, $precision10 = 10) {
$timestamp10 = strtotime($timestamp10);
$precision10 = 60 * $precision10;
return date('H:i:s', round($timestamp10 / $precision10) * $precision10);
}
$Billable_Time = roundTime10($Billable_Time);
function roundTime15($timestamp15, $precision15 = 15) {
$timestamp15 = strtotime($timestamp15);
$precision15 = 60 * $precision15;
return date('H:i:s', round($timestamp15 / $precision15) * $precision15);
}
$Billable_Time = roundTime15($Billable_Time);
echo $Billable_Time;
could anyone show me a better way to do this or ad to my way to make it work?
The basic logic for this is "subtract five minutes, divide by 15 to an integer result then add one and multiply by fifteen".
01:04:00 = 01:00:00: 4-5 = -1. -1/15 = -0.xxx (round down to -1). (-1+1)*15 = 0
01:20:00 = 01:30:00: 20-5 = 15. 15/15 = 1 (round down to 1). (1+1)*15 = 30
01:19:00 = 01:15:00: 19-5 = 14. 14/15 = 0.xxx (round down to 0). (0+1)*15 = 15
I don't have a PHP interpreter handy so won't write the code but you need to replace "round($timestamp15 / $precision15) * $precision15" with something that uses the logic above. You'll want to use the PHP floor() as it rounds negatives down while round() rounds towards or away from zero.
I'm trying to calculate my min/km average of my total run.
Currently I have ran ($this->totaltime) 3113 seconds, ($this->distance) 6313.59 and my pace should be 08:13 min/km (yes this was very slow!)
Code I'm using:
// Pace
function getPace() {
$dis_pace = $this->distance / 1000;
$pace = $this->totaltime / $dis_pace / 60;
return $pace;
}
I think I'm missing something here..
[EDIT] The problem is I get 8.21 but need to have the results in time 08:13. [/EDIT]
Anybody?
Try this one :
// Pace
function getPace() {
$dis_pace = $this->distance / 1000;
//getting seconds per km
$pace = $this->totaltime / $dis_pace;
//getting minutes from $pace
$min = floor($pace / 60);
//adding 0 before, if lower than 10
$min = ($min > 10) ? $min : '0'.$min;
//getting remaining seconds
$sec = $pace % 60;
//adding 0 before, if lower than 10
$sec = ($sec > 10) ? $sec : '0'.$sec;
return $min.":".$sec;
}
I have data for 2 profiles that I would like to compare.
Each profile has a 'total points' value and a 'points per day' value and I would like to calculate how many days it would take the hare to overtake the tortoise.
$hare_points = 10000;
$hare_ppd = 700;
$tortoise_points = 16000;
$tortoise_ppd = 550;
What would be the most efficient way to determine how many days it would take for the hare to catch up with the tortoise? My first through was to run a loop to just count through the days, but very quickly realised there must be an efficient algorithm out there that will not want to destroy the server that it is running on lol
Assuming ppd is points per day:
<?php
$hare_points = 10000;
$hare_ppd = 700;
$tortoise_points = 16000;
$tortoise_ppd = 550;
$hare_diff = $tortoise_points - $hare_points;
$hare_ppd_diff = abs($tortoise_ppd - $hare_ppd);
$days = $hare_diff/$hare_ppd_diff;
echo $days; // 40
/* Test:
* 40 * 700 = 28000; hare
* 40 * 550 = 22000; tortoise
*
* hare_p = 28000 + 10000 = 38 000
* toit_p = 22000 + 16000 = 38 000
*
* So the math is right. On 40th day, they are equal
*
*/
It's a simple set of equations to solve.
hare_total = hare_points + hare_ppd * days
tortoise_total = tortoise_points + tortoise_ppd * days
You're trying to find out the day the points are the same, so:
hare_total = tortoise_total
hare_points + hare_ppd * days = tortoise_points + tortoise_ppd * days
hare_points - tortoise_points = (tortoise_ppd - hare_ppd) * days
So there's your answer:
$days = ($hare_points - $tortoise_points) / ($tortoise_ppd - $hare_ppd)
Just plug that into your function and round up / down to an integer depending on how you want to interpret the answer.
Heres the code I have at the moment, its all working as intended, however, the cumulative total isn't working, and I'm positive I'm doing something absolutely stupid.
assume period = 20
assume inflation = 3
assume nightlycost = 100
assume nights = 7
$yearlycost = $nightlycost*$nights;
while ($period > 0) {
$period = $period-1;
$yearlyincrease = ($inflation / 100) * $yearlycost;
$nightlyincrease = ($inflation / 100) * $nightlycost;
$nightlycost = $nightlycost + $nightlyincrease;
$yearlycost = ($yearlycost + $yearlyincrease) + $yearlycost;
}
Result:
Nightly Hotel Rate in 20 years: $180.61 - <?php echo round($nightlycost, 2); ?> correct
Weekly Hotel Rate in 20 years: $1264.27 - <?php echo round($nightlycost, 2) * 7; ?> correct
Total cost to you over 20 years: $988595884.74 - <?php echo round($yearlycost, 2); ?> incorrect
Everything outputs correctly and as expected, except for the yearly cumulative cost. It should take the previous yearly cost and add that years cost+inflation.
Example: first year is 700, so second year should be 700 + 700 + 21 (21 is 3%, the inflation for that year). Second year cumulative total is thus: 1421. Third year will be 1421 + 721 (last years total) + 3% of 721.
Hopefully this is clear enough for you to see where I'm going wrong. Thanks!
I find it hard to understand where your code goes wrong, but my intuition is that the last line in your loop body should have a multiplication.
Basically, you have a base cost for period 0. Then you want to calculate the cumulative cost given inflation after X years. That cost is (pseudocode)
base = nightlycost + nights
infl = 1.03
cumulative = base + base*infl + base*infl^2 + base*infl^3 + ... + base*infl^periods
The last expression can be simplified to
cumulative = base*((1-infl^periods)/(1-infl))
(This holds according to Eq. 4 here: http://mathworld.wolfram.com/ExponentialSumFormulas.html)
Example:
$base = 100*7;
$infl = 1.03; // 3% of inflation/year
$periods = 2;
$cumulative = $base * (1-pow($infl, $periods))/(1-$infl);
print "Cumulative cost after $periods is $cumulative\n";
// Let's try with three periods.
$periods = 3;
$cumulative = $base * (1-pow($infl, $periods))/(1-$infl);
print "Cumulative cost after $periods is $cumulative\n";
Output:
Cumulative cost after 2 is 1421
Cumulative cost after 3 is 2163.63