Print next days after 18/01/2016

Asked

Viewed 57 times

2

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

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 in: PHP: The Right Way

1

Hello, that should do it:

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

The result of this 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"
}

php by does not come with Brazilian time, so arrange it use this function date_default_timezone_set("America/Sao_Paulo")

Browser other questions tagged

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