loop to get date

1

Hello everyone, I have a banking ticket system. I need to perform a card, but I am not able to execute the strtotime function properly to generate the expiration. = \

<?php
$parcelas =11;
$now = "18/07/2018";
for($i = 0, $meses = 1 ;$i < $parcelas; $i++, $meses++){
$vencimento = date("d/m/Y", strtotime("$now +".$meses." months"));
} 
?>

the above result begins with: 01/01/1970 The expected result would be 18/07/2018 + 30 days and so on. Thank you guys! :)

    
asked by anonymous 19.07.2018 / 04:54

2 answers

1

First problem is the current date format in the variable $now , you need to convert it to a format without the / :

$now="18/07/2018";
$now=str_replace("/","-",$now);

Then you will convert $now to the default format Y-m-d :

$now=date("Y-m-d",strtotime($now));

Now, you can add the months to that date, within the for loop:

for($i = 0, $meses = 1 ;$i < $parcelas; $i++, $meses++){
    $vencimento=date("d/m/Y",strtotime("$now + $meses month"));
    echo $vencimento."<br>";
}
    
19.07.2018 / 05:17
2

Date format dd/mm/aaaa does not exist, only mm/dd/aaaa , so for PHP, you are informing the 7th day of the 18th month, which does not exist, so the date goes to the year 1970.

Replace with 18-07-2018 that should work.

    
19.07.2018 / 05:13