有没有办法在每个月后在PHP中增加一个数字

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"

https://3v4l.org/51XMD

I've made few assumptions:

  • you need this auto increment value somewhere in your code which will be executed as a part of some script
  • your initial date and 500 value belong to the past, I mean any current date will be greater than that initial date

https://ideone.com/iobOZO

$initialDate = strtotime("2018-12-01");

$years = (date("Y") - date("Y", $initialDate));

$number = 500 + $years*12 
          + date("m") - date("m", $initialDate);

echo $number;