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
Related
I'm creative a forum and want to calculate the frequency of new posts per day. So, each post has timestamps:
$post_1 = 1526083200;
$post_2 = 1524083200;
$post_3 = 1523083200;
$post_4 = 1522083200;
What calculation do I do to find out how often posts are submitted per day. Example final output:
echo 'Every '. $frequency .' day(s)';
You can maybe try something like this :
$post_1 = 1526083200;
$post_2 = 1524083200;
$post_3 = 1523083200;
$post_4 = 1522083200;
// I add all the value in an array then sort the array to get the min and max value
$date_array = [$post_1, $post_2, $post_3, $post_4];
sort($date_array);
// Now I can select the min date and the max date
$min_date = $date_array[0];
$max_date = $date_array[count($date_array) - 1];
// I calculate the diff to get the number of day during this period
$datediff = $max_date - $min_date;
// I divide this value with the number or article post during this period
$frequency = $datediff / count($date_array);
// Now I transform this value in number of day
$frequency = round($frequency / (60 * 60 * 24));
With your example, this is what you got :
Number of articles : 4
Min date : 2018-03-26
Max date : 2018-05-12
Number of day of the period : 46
Frequency : 12
That sound good for me with those value , one article every 12 days.
Is it what you are looking for?
assuming you want the general frequency:
frequency = 1 / period
period = average time between two posts = time between oldest and newest post / number of post - 1
time between oldest and newest post = newest post - oldest post
In your example:
$post_1 = 1526083200;
$post_2 = 1524083200;
$post_3 = 1523083200;
$post_4 = 1522083200;
time between oldest and newest posts = 1526083200 - 1522083200 = 4000000 seconds = 46,2962963 Days
period = 46,2962963/3 = 15.4320987667 Days (There is on average 15 days between two posts)
frequency = 1 / 15.4320987667 = 0.06479999999 (There is on average one post each 0.0648 Days)
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
I want to calculate how many months have passed since an X1 date to an X2 date, and to calculate how much will I have to pay with a given monthly interest rate.
The script should: Get the amount of a debt (capital), the month and year when the debt was originated and up until it should get calculated. Not the day, just the month and the year.
And the output should be the total generated in interests, and the total months between the two dates.
I have these initial variables:
$tMonth = $_POST['tmes'];
$tYear = $_POST['tanio'];
$interes = $_POST['interes'];
$fMonth = $_POST['fmes'];
$fYear = $_POST['fanio'];
$capital = $_POST['capital'];
And this is what I've done:
if($_SERVER['REQUEST_METHOD']=='POST') {
//I try and obtain how many months do I have between the two months
$mesesEnAnios = (($tYear - $fYear) -1) * 12;
$mesesScattered = (12 - $fMonth) + $tMonth;
$mesesTotales = $mesesEnAnios + $mesesScattered;
//Then I do calculate the interest I'll have to pay
$totalCapital = $capital * ($interes * $mesesTotales) / 100;
echo 'Son $'.$totalCapital.' en '.$mesesTotales.' meses.';
...
I've tried this script and it works. But I don't know much about PHP (nor math) and I don´t know if this will always work.
I've researched other solutions here at SO, but I think mine is a little easier - at least I do understand it :) - maybe it´s because it's not correct?
No, it is wrong.
$mesesEnAnios = (($tYear - $fYear) -1) * 12;
That is for example: (2013 - 2012 - 1) = 0
0 * 12 = 0, while its obvious 12.
Then you do
$mesesScattered = (12 - $fMonth) + $tMonth;
So, lets say we have 05-2013 (month - year) and 05-2012. The result should be: 12 months. Lets assume you fixed the first line I pointed out, we get 12 months from that. On your second line, you get 12 - 5, which is 7. Result 7+12 = 19.. which is again obvious wrong.
To make it easy, ill just give you the code..
$months = (($tYear - $fYear ) * 12) + ($tMonth- $fMonth);
You first check if there are any extra's years and do this amount * 12 for the 12 months we have. 0 years give 0 months, 1 year gives 12 months etc. After that we check the months. If the first value is 8 months and the second value is 6 months, we have 2 months in total and add this value on top of the value we got from the years calculation.
That is the month part, for the other part I cannot help you since I do not know the calculation. Yet assuming you have 0 experience in math and PHP (like you said), I would recommend to manually calculate it, and compare it with the result from the script.
The PHP script captures the values and calculates the future value. working backwards from the last month to the first, how could I compute the investment for the previous month and display the months number and the value all the way back to the first investment (Unknown). I've already figured out that I'll have to uses a formula like this "months value = (months value)/(1 + rate)" but after that I hit a road block. basically I'm trying how to figure how much do you invest today to have a balance value of $XXX after xx years at a x% interest rate?
<?php
// get the data from the form
$investment = $_POST['investment'];
$interest_rate = $_POST['interest_rate'];
$years = $_POST['years'];
// calculate the future value
$future_value = $investment;
for ($i = 1; $i <= $years; $i++) {
$future_value = ($future_value + ($future_value * $interest_rate *.01));
}
// apply currency and percent formatting
$investment_f = '$'.number_format($investment, 2);
$yearly_rate_f = $interest_rate.'%';
$future_value_f = '$'.number_format($future_value, 2);
?>
Your formula is basically this:
Start with X
For each of the Y years, increase X by Z%
This is a simple function: result = X * (1+Z%)^Y
So you know there's a specific result you want, and you know the number of years and the interest rate, and you want to calculate the base amount. Easy: X = result / (1+Z%) ^ Y
Using your variable names, you'd get
$investment = $future_value / pow(1+$interest_rate/100,$years);
Done!
I have this project that has a set price for a certain amount of hours, I need to add more money to the overall total for each hour after 5.
My script already calculated the time by counting the hours:
Which outputs like, "15" or "9.5" or "3.5" or "7" etc.
Let's say that 5 hours is £50, how would I add an additional £15 for every hour over the 5 hour limit.
This includes if a user going over by ".5"
(So 5.5 hours would be £65 and 6 would be £65)
Any help would be great, Thanks!
Subtract 5 from the number of hours, ceil() / round() the number and multiply by 15?
You ceil() if you want any part of an hour to be charged, whereas you round() if you want to charge only if the fraction of the hour is .5 or higher.
$hours = 5.5;
$amount = 50 + ceil(max(0,$hours-5)) * 15; # 65
I assume that anything belove 5 hours is 50.
$price = $val <= 5 ? 50 : (50 + (int) ceil($val-5) * 15)
Assuming I've understood you correctly, are you looking for something like this:
$total = 50;
if($hours > 5) {
$total += ceil($hours - 5) * 15;
}