loop to get date

Asked

Viewed 24 times

1

Hello guys, I have a boleto Bancario system. I need to perform a card, but I am not able to execute the strtotime function correctly to generate the maturity. =\

<?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 starts with: 01/01/1970 The expected result would be 18/07/2018 + 30 days and so on. Personal thank you! :)

2 answers

2

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

Replace to 18-07-2018 that should work.

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 for the standard format Y-m-d:

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

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.