Go through dates by printing a field for each one

2

I'm making a system a bit like a calendar / schedule. In it I have a semestre table that has a data de incio field and a data fim field and I need to scroll through those dates by printing the name of each day in the head of a table and a button for each of those days so that the user can register a new entry that day or not.

How to go through these days? I've tried foreach but it did not work, I also tried logic with for and I could not ...

Some of the code I've done so far:

if(!isset($_SESSION)) {
   session_start();
} 

$idSemestre = $_SESSION['SemestreGeral'];

$oSemestre = new semestresclass();
$oSemestre -> listarEdicao($idSemestre);  

$array = mysql_fetch_array($oSemestre->retorno());

$start_date = $array['DataDeInicio'];
$end_date = $array['DataDeTermino'];

$dataInicio = strtotime($start_date);
$dataFim = strtotime($end_date);

foreach ?????????????????????
    
asked by anonymous 25.10.2014 / 22:20

2 answers

2

As of php5.3, the class DatePeriod allows you to manipulate between date periods. It is enough to inform the start date, end date and the period that will be modified in case P1D (one day) Other values available are:

Y   Anos
M   Meses
D   Dias
W   Semanas
H   Horas
M   Minutos
S   Segundos

Example

<?php

$inicio = new DateTime('2014-10-26');
$fim = new DateTime('2014-10-31');
$fim->modify('+1 day');

$interval = new DateInterval('P1D');
$periodo = new DatePeriod($inicio, $interval ,$fim);

foreach($periodo as $data){
    echo $data->format("l") . ' | '.$data->format("d/m/Y"). '<br>';

}
    
25.10.2014 / 23:43
3

Create an object of type \DatePeriod by passing the start and end date parameters as well as a% of type "P1D", that is, a period of one day between dates.

Your code looks like this:

$dataInicio = new \DateTime(strtotime($start_date));
$dataFim    = new \DateTime(strtotime($end_date));
$periodo    = new \DatePeriod($dataInicio, new \DateInterval("P1D"), $dataFim);

foreach ($periodo as $data) {
    echo $data->format('d/m/Y');
}
    
25.10.2014 / 23:43