Print the next days after the 18/01/2016

2

I have to make a code where I put a date and I need to know what the next 5 days are. For example, today is 18/01/2016 the next few days are 19/01/2016, 20/01/2016, 21/01/2016, 01/22/2016 and 23 / 01/2016 . I need to pick up the next 5 days of a certain date. How can I do this?

    
asked by anonymous 19.01.2016 / 02:08

2 answers

2
<?php
$today = getdate(); // pega data de hoje

$raw = "{$today['yday']}. {$today['mon']}. {$today['year']}"; // string da data

$start = DateTime::createFromFormat('d. m. Y', $raw); //formata a data

echo "Data Inicial: " . $start->format('d/m/Y') . "\n"; // exibe data inicial

// cria uma cópia de $start e adiciona 6 dias, pois contamos com hoje
$end = clone $start;
$end->add(new DateInterval('P6D'));


$periodInterval = DateInterval::createFromDateString('tomorrow'); // pega todos os "amanhãs"
$periodIterator = new DatePeriod($start, $periodInterval, $end, DatePeriod::EXCLUDE_START_DATE); // cria período excluindo data inicial

foreach($periodIterator as $date) {
    //mostra cada data no período
    echo "<br>".$date->format('d/m/Y') . " ";
}

Learn more at: PHP: The Right Way

    
19.01.2016 / 02:31
1

Hello, this should resolve:

for ($i=1; $i <= 5; $i++) { 
    $proximosDias[] = date('d/m/Y', strtotime(" +$i day"));
}

The result will be:

array(5) {
[0]=>
string(10) "19/01/2016"
[1]=>
string(10) "20/01/2016"
[2]=>
string(10) "21/01/2016"
[3]=>
string(10) "22/01/2016"
[4]=>
string(10) "23/01/2016"
}

The php by does not come with the Brazilian time, so you can use this function date_default_timezone_set("America/Sao_Paulo")

    
19.01.2016 / 02:29