AZRUL HARIS
AZRUL HARIS

Reputation: 7

PHP How To Calculate Yearly Increase by Given Start and End Year

By given 2 range of year, current population and target population, how to get total population every year by using PHP?

Example:

Year | Population

2014 | 100000

2018 | 132000

On paper calculation like this:

132000 / 100000 = 1.0718

so we will get result every year (On paper):

2014 = 100000

2015 = 107187 (100000 * 1.0718)

2016 = 114890 (107187 * 1.0718)

2017 = 123147 (114890 * 1.0718)

2018 = 132000

How to hold previous year variable to get result as above?

This is my PHP code:

for($i > $start; $i < $end; $i++) {
    $this->ProjectPopulation->create(); 
    $increase = array(
        'project_id' => $project_id,
        'year' => $i,
        'percent_increase' => $this->percentage_increase($current_population, $target_population, $year),
        'population' => ??? // Problem here
     );
     $this->ProjectPopulation->save($increase); 
 }

Thanks

Upvotes: 0

Views: 271

Answers (2)

AZRUL HARIS
AZRUL HARIS

Reputation: 7

Solve

private function power($current_population, $target_population, $year) {
    $devide = $target_population / $current_population; 
    $pwrdevider = 1 / $year;
    $power = pow($devide, $pwrdevider); 
    return $power;
}

private function percentage_increase($current_population, $target_population, $year) {
    $devide = $target_population / $current_population; 
    $power = pow($devide, 1 / $year);
    $increase = ($power - 1) * 100;
    return $increase;
}

    $start = 2014;
    $end = 2018;
    $diff = $end - $start; 
    for($i = 1, $data = 100000; $i <= $diff; $i++) {  
        $data *= $this->power(100000, 132000, $diff);

        $increase = array(
            'project_id' => 1,
            'year' => $start += 1,
            'percent_increase' => $this->percentage_increase(100000, 132000, $diff),
            'population' => $data 
        );
        print_r($increase); 
    }

This is my result

Array
(
   [project_id] => 1
   [year] => 2015
   [percent_increase] => 7.1873373728262
   [population] => 107187.33737283
)
Array
(
   [project_id] => 1
   [year] => 2016
   [percent_increase] => 7.1873373728262
   [population] => 114891.25293076
)
Array
(
   [project_id] => 1
   [year] => 2017
   [percent_increase] => 7.1873373728262
   [population] => 123148.87489076
)
Array
(
   [project_id] => 1
   [year] => 2018
   [percent_increase] => 7.1873373728262
   [population] => 132000
)

Thanks all

Upvotes: 0

hlh3406
hlh3406

Reputation: 1400

There are two resources I'd recommend you taking a look at:

http://php.net/manual/en/ref.math.php

and

http://www.w3schools.com/php/php_ref_math.asp

You can solve this by doing the calculation each time, but it would be faster to use a loop in order to iterate through them all.

So something like this:

$population = 100000;
$year = 2014;

//I'd print the first one out prior to the loop, or you could put an if($year == 2014) in the loop, your choice really. 

for(i= 0; i < 4; i++)
{
   //do calculation in here.
   echo "The current years is" . $year;  
   $yearsCalculation = $population * 1.0718;  

   //then save that value for the next time around the loop. 
   $population = $yearsCalculation ; 
   echo $yearsCalculation ;      

   //increase years each time round loop
   $year++;
}

If you then want to store it in a MySQL database you might want to have a look at this tutorial:

http://www.w3schools.com/php/php_mysql_insert.asp

Hope that helped!

Upvotes: 1

Related Questions