Comparing dates of an array in PHP

0

I'm trying to compare the dates coming from an array of the database so I can organize them within a timeline. The problem is that I can not compare dates to know where to put each entry ...

Maybe the problem is that within the array the dates are in date format, but if so, how to compare?

Follow the code:

<tbody>
    <tr>
        <td><center>Manhã</center></td>
                <?php 
                    foreach($periodo as $data){
                        while ($arrayBancas = mysql_fetch_array($oBanca->retorno())){
                            if ($arrayBancas['data'] == $data->format("Y-m-d")){
                                echo '<td>teste</td>';
                            }
                        }
                    }
                ?> 
     </tr>

Variable content:

(Period)

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'];

$inicio = new DateTime($start_date);
$fim = new DateTime($end_date);
$fim->modify('+1 day');

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

(Banking Array)

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

$idSemestre = $_SESSION['SemestreGeral'];

$oBanca = new bancasclass();
$oBanca -> listar ($idSemestre);   

$arrayBancas = mysql_fetch_array($oBanca->retorno())

Here is the value of var_export of the variables:

arrayBancas: Null, 2 => NULL, 'time' => NULL, 3 = 'NULL', 'NULL', 'NULL', ' & quot; '316', 'room' = > '316',)

Date: DateTime :: __ set_state (array ('date' => '2014-06-10 00:00:00', 'timezone_type' => 3, 'timezone' => 'GMT',))

    
asked by anonymous 07.11.2014 / 23:03

1 answer

1

With recent issues you have two issues:

  • Your query is not returning what you expect
  • You are comparing strings hoping to get notions of time
  • The first problem can only be solved because it would run away from the scope of the topic (but this does not prevent it from creating another case that has difficulties).

    The second problem is very simple to solve, but requires you to repair its structure in the database.

    By output of $ arrayBancas you appear to have a field of type DATETIME but do not have the time. Or you fix the way you enter the data, so that the hours arrive or change the field type.

    DateTime objects are comparable with conventional operators (& lt ;, & gt ;, == and! ==) so once you have corrected the previous problem, just create a DateTime object with the operand on the left side and make the comparison: p>

    if( DateTime::createFromFormat( 'Y-m-d', $arrayBancas['data'] ) -> format( 'Y-m-d' ) == $data -> format( 'Y-m-d' ) ) {
        echo '<td>teste</td>';
    }
    

    If you choose to fix the way you enter the data and get to the times you change the format of Ymd to Ymd H: i: s . p>

    Remembering that the DateTime :: createFromFormat () method was introduced in PHP 5.3.

        
    08.11.2014 / 17:58