I'm working on a project in PHP. What i want to do is to increment a number (Like 500) by itself every month. Is there any possible way to achieve this?
I don't know your exact algorithm, but guessing the below:
$start = new DateTime('2009-10-11');
$end = new DateTime('2010-12-23');
$diff = $end->diff($start);
$months = ($diff->y * 12) + $diff->m;
$rollup = 0;
var_dump("months: $months");
while ($months-- > 0) {
echo "rollup: $rollup".PHP_EOL;
$rollup += $rollup ?: 500;
}
var_dump("result: $rollup");
Gives:
string(10) "months: 14"
rollup: 0
rollup: 500
rollup: 1000
rollup: 2000
rollup: 4000
rollup: 8000
rollup: 16000
rollup: 32000
rollup: 64000
rollup: 128000
rollup: 256000
rollup: 512000
rollup: 1024000
rollup: 2048000
string(15) "result: 4096000"
I've made few assumptions:
500
value belong to the past, I mean any current date will be greater than that initial date$initialDate = strtotime("2018-12-01");
$years = (date("Y") - date("Y", $initialDate));
$number = 500 + $years*12
+ date("m") - date("m", $initialDate);
echo $number;