Problème avec une condition DATE

Résolu
Bonjour à tous,

J'aimerai modifier ma condition pour faire en sorte de passer le fond en rouge de $désignation_diag dans mon tableau si ma $date-echeance a dépassée de 7 jours la date du jour. J'ai tenté plusieurs solutions avec la fonction strtotime, mais j'ai pas trouvé la bonne formule pour que cela fonctionne.

Merci pour votre aide.

$objDate_ech = New DateTime($data["date_echeance"]);

if(date("d/m/Y") == $date_echeance , strtotime('+7 day') = $objDate_ech->format("d/m/Y")) {
        echo '<td align="center" bgcolor="#FF9900">'.$data["designation_diag"].'</td>';
}elseif(date("d/m/Y") > $date_echeance = $objDate_ech->format("d/m/Y")) {
        echo '<td align="center" bgcolor="#FF0000">'.$data["designation_diag"].'</td>';
}else {
        echo '<td align="center" bgcolor="#CCCCCC">'.$data["designation_diag"].'</td>';
}

15 réponses

  1. Merci Varaldi Q pour la réponse,
    mais j'ai ce message d'erreur :
    Parse error: syntax error, unexpected ','

    et si je supprime la virgule j'ai :
    Parse error: syntax error, unexpected T_STRING

    Et j'aimerai resté avec un affichage en rouge que si la $date_echeance est égale à la date du jour.
    1. Autant pour moi, je n'avais pas lu le reste du code.

      Sinon, tu as essayé de faire un mktime de ta date == mktime de l'autre date + strtotime ?

      1. Contributeur
        Bonjour,

        Où as-tu vu la syntaxte :
        if(var1 == var2 , var3 = var4) 

        ???

        1. Bon, je galère toujours...

          Il semble que la fonction strtotime ne peut pas être utilisée et attribuée à une variable, si quelqu'un a déjà eu le cas.

          Merci pour les infos.
          1. tu ne peux pas faire des comparaisons > avec des variables string de la forme d /m /Y

            essayes comme ça en passant par le timestamp

            $arrayDate_ech = explode('/',$data["date_echeance"]);
            $timestamp_Date_ech=mktime(0,0,0,$arrayDate_ech[0],$arrayDate_ech[1],$arrayDate_ech[2];
            $timestamp_today=time();
            
            if($timestamp_Date_ech > $timestamp_today +(86400*7)){
            	echo '<td align="center" bgcolor="#FF9900">'.$data["designation_diag"].'</td>';
            }else{
            	echo '<td align="center" bgcolor="#CCCCCC">'.$data["designation_diag"].'</td>';
            }
            1. Merci Alain_42,

              En fait, je veux ajouter 7 jours la variable $date_echeance et pas sur la date du jour.
              Je veux que la couleur des cellules designation_diag change en fonction de la $date_echeance par rapport à la date du jour et :
              - passe en rouge quand la $date_echeance a dépassé de 7 jours la date du jour
              - passe en orange quand $date_echeance et égale à la date du jour
              - passe en gris clair dans les autres cas

              Je pense que tu as la bonne solution avec timestamp, j'ai simplement fermer la parenthése après $arrayDate_ech[2]

              @+
              1. ok

                <?php
                $arrayDate_ech = explode('/',$data["date_echeance"]);
                $timestamp_Date_ech=mktime(0,0,0,$arrayDate_ech[0],$arrayDate_ech[1],$arrayDate_ech[2]);
                $timestamp_today=time();
                
                if($timestamp_Date_ech +(86400*7) > $timestamp_today ){
                	// oui effectivement
                	echo '<td align="center" bgcolor="#FF0000">'.$data["designation_diag"].'</td>';
                }if($timestamp_Date_ech ==  $timestamp_today){
                	echo '<td align="center" bgcolor="#FF9900">'.$data["designation_diag"].'</td>';
                }else{
                	echo '<td align="center" bgcolor="#CCCCCC">'.$data["designation_diag"].'</td>';
                }
                1. Alain_42, merci pour ton aide, ça ne fonctionne pas et je ne trouve pas l'erreur que je fais... grrrrrrrrrr !

                  Voici mon code :

                  <?php
                  // Infos bdd
                  $host = 'xxxxxxxxxx';
                  $user = 'xxxxxxxxxx';
                  $pass = 'xxxxxxxxxx';
                  $db   =  'xxxxxxxxxx';
                  
                  // connection à la bdd
                  $link = mysql_connect ($host,$user,$pass) or die ('Erreur : '.mysql_error() );
                  mysql_select_db($db) or die ('Erreur :'.mysql_error());
                  
                  
                  // requête SQL
                  $select = ("select * from diagnostic ORDER BY date_crea DESC");
                  $result = mysql_query($select,$link) or die ('Erreur : '.mysql_error() );
                  $total = mysql_num_rows($result);
                  
                  // si on a récupéré un résultat on l'affiche.
                  if($total) {
                  // debut du tableau
                          echo '<table bgcolor="#FFFFFF">'."\n";
                  // première ligne on affiche les titres 
                          echo '<tr>';
                          echo '<td align="center" bgcolor="#669999"><b>Diagnostic</b></td>';
                  		echo '<td align="center" bgcolor="#669999"><b>Date d\'échéance</b></td>';
                  		echo '</tr>'."\n";
                      
                  // lecture et affichage des résultats   
                      while($data = mysql_fetch_array($result)) {
                  
                  
                  $objDate_ech = New DateTime($data["date_echeance"]); 
                   
                          
                  $arrayDate_ech = explode('/',$data["date_echeance"]);
                  $timestamp_Date_ech=mktime(0,0,0,$arrayDate_ech[0],$arrayDate_ech[1],$arrayDate_ech[2]);
                  $timestamp_today=time();
                  
                  if($timestamp_Date_ech +(86400*7) > $timestamp_today ){
                  	// oui effectivement
                  	echo '<td align="center" bgcolor="#FF0000">'.$data["designation_diag"].'</td>';
                  }if($timestamp_Date_ech == $timestamp_today){
                  	echo '<td align="center" bgcolor="#FF9900">'.$data["designation_diag"].'</td>';
                  }else{
                  	echo '<td align="center" bgcolor="#CCCCCC">'.$data["designation_diag"].'</td>';
                  }
                  
                  
                      echo '<td align="center" bgcolor="#CCCCCC">'.$date_echeance = $objDate_ech->format('d/m/Y').'</td>';
                  	echo '</tr>'."\n";
                  }
                      echo '</table>'."\n";
                  // fin du tableau.
                  }
                  
                  // on libère le résultat
                  mysql_free_result($result);
                  
                  // DECONNEXION MYSQL
                  close();
                  ?>
                  
                  1. essayes comme ça, j'avais qqs erreurs
                    <?php 
                    // Infos bdd 
                    $host = 'xxxxxxxxxx'; 
                    $user = 'xxxxxxxxxx'; 
                    $pass = 'xxxxxxxxxx'; 
                    $db   =  'xxxxxxxxxx'; 
                    
                    // connection à la bdd 
                    $link = mysql_connect ($host,$user,$pass) or die ('Erreur connexion serveur: '.mysql_error() ); 
                    mysql_select_db($db) or die ('Erreur selection base:'.mysql_error()); 
                    
                    
                    // requête SQL 
                    $select = "SELECT * FROM diagnostic ORDER BY date_crea DESC"; 
                    $result = mysql_query($select,$link) or die ('Erreur requette: '.mysql_error() ); 
                    $total = mysql_num_rows($result); 
                    
                    // si on a récupéré un résultat on l'affiche. TESTES si sup à 0 
                    if($total >0) { 
                     // debut du tableau 
                        echo '<table bgcolor="#FFFFFF">'."\n"; 
                     // première ligne on affiche les titres  
                         echo '<tr>'; 
                        echo '<th bgcolor="#669999">Diagnostic</td>'; 
                     echo '<th bgcolor="#669999">Date d\'échéance</td>'; 
                     echo '</tr>'; 
                         
                     // lecture et affichage des résultats   
                     $timestamp_today=mktime(0,0,0,date('n'),date('j'),date('Y')); //ça on peut le sortir de la boucle pour éviter le calcul à chaque tour 
                      
                      while($data = mysql_fetch_array($result)) { 
                       
                     echo '<tr>'; 
                       
                      //$objDate_ech = New DateTime($data["date_echeance"]);  
                      $arrayDate_ech = explode('/',$data["date_echeance"]); 
                      $timestamp_Date_ech=mktime(0,0,0,$arrayDate_ech[1],$arrayDate_ech[0],$arrayDate_ech[2]); //la j'avais une petite erreur 
                      if($timestamp_Date_ech +(86400*7)< $timestamp_today ){    
                       echo '<td align="center" bgcolor="#FF0000">'.$data["designation_diag"].'</td>'; 
                      }if($timestamp_Date_ech == $timestamp_today){ 
                       echo '<td align="center" bgcolor="#FF9900">'.$data["designation_diag"].'</td>'; 
                      }if($timestamp_Date_ech != $timestamp_today && $timestamp_Date_ech > $timestamp_today){  
                       echo '<td align="center" bgcolor="#CCCCCC">'.$data["designation_diag"].'</td>'; 
                      } 
                        
                      echo '<td align="center" bgcolor="#CCCCCC">'$data["date_echeance"].'</td>';  
                      echo '</tr>'."\n"; 
                      
                    } 
                        echo '</table>'."\n"; 
                    // fin du tableau. 
                    } 
                    
                    // on libère le résultat 
                    mysql_free_result($result); 
                    
                    // DECONNEXION MYSQL 
                    close(); 
                    
                    ?>
                    1. ça fonctionne toujours pas... c'est pas dans le formatage de la date mktime ?

                      m pour le mois
                      d pour le jour
                      y pour l'année
                      1. qu'est ce qui ne marche pas ?

                        j'ai mis n au lieu de m pour avoir sans le zero
                        j idem
                        et Y pour l'année en 4 chiffres

                        j'ai fait l'essai en local chez moi avec les valeurs dans un array pour simuler les valeurs de la BDD ça marche

                        mais est ce que tes dates sont bien au format dd/mm/yyyy
                        dans ta BDD
                        1. donc c'est normal que ça ne marche pas

                          <?php 
                          // Infos bdd 
                          $host = 'xxxxxxxxxx'; 
                          $user = 'xxxxxxxxxx'; 
                          $pass = 'xxxxxxxxxx'; 
                          $db   =  'xxxxxxxxxx'; 
                          //fonction php transfo format date
                          function date_mysql_fr($date_mysql){
                          	$array_date_mysql=explode('-',$date_mysql);
                          	$date_fr=$array_date_mysql[0].'/'.$array_date_mysql[1].'/'.$array_date_mysql[1];
                          	return $date_fr;
                          }
                          // connection à la bdd 
                          $link = mysql_connect ($host,$user,$pass) or die ('Erreur connexion serveur: '.mysql_error() ); 
                          mysql_select_db($db) or die ('Erreur selection base:'.mysql_error()); 
                          
                          
                          // requête SQL 
                          $select = "SELECT * FROM diagnostic ORDER BY date_crea DESC"; 
                          $result = mysql_query($select,$link) or die ('Erreur requette: '.mysql_error() ); 
                          $total = mysql_num_rows($result); 
                          
                          // si on a récupéré un résultat on l'affiche. TESTES si sup à 0 
                          if($total >0) { 
                           // debut du tableau 
                              echo '<table bgcolor="#FFFFFF">'."\n"; 
                           // première ligne on affiche les titres  
                               echo '<tr>'; 
                              echo '<th bgcolor="#669999">Diagnostic</td>'; 
                           echo '<th bgcolor="#669999">Date d\'échéance</td>'; 
                           echo '</tr>'; 
                               
                           // lecture et affichage des résultats   
                           $timestamp_today=mktime(0,0,0,date('n'),date('j'),date('Y')); //ça on peut le sortir de la boucle pour éviter le calcul à chaque tour 
                            
                            while($data = mysql_fetch_array($result)) { 
                             
                           echo '<tr>'; 
                             
                            //$objDate_ech = New DateTime($data["date_echeance"]);  
                            //changement ci dessous
                            $arrayDate_ech = explode('-',$data["date_echeance"]); 
                            $timestamp_Date_ech=mktime(0,0,0,$arrayDate_ech[2],$arrayDate_ech[0],$arrayDate_ech[0]); 
                            
                            /////
                            if($timestamp_Date_ech +(86400*7)< $timestamp_today ){    
                             echo '<td align="center" bgcolor="#FF0000">'.$data["designation_diag"].'</td>'; 
                            }if($timestamp_Date_ech == $timestamp_today){ 
                             echo '<td align="center" bgcolor="#FF9900">'.$data["designation_diag"].'</td>'; 
                            }if($timestamp_Date_ech != $timestamp_today && $timestamp_Date_ech > $timestamp_today){  
                             echo '<td align="center" bgcolor="#CCCCCC">'.$data["designation_diag"].'</td>'; 
                            } 
                              //modif ici aussi
                            echo '<td align="center" bgcolor="#CCCCCC">'.date_mysql_fr($data["date_echeance"]).'</td>';  
                            echo '</tr>'."\n"; 
                            
                          } 
                              echo '</table>'."\n"; 
                          // fin du tableau. 
                          } 
                          
                          // on libère le résultat 
                          mysql_free_result($result); 
                          
                          // DECONNEXION MYSQL 
                          close(); 
                          
                          ?>
                          
                          
                          1. Alain_42,

                            J'ai testé avec ton code et j'ai cette erreur :
                            Fatal error: Call to undefined function date_mysql_fr()

                            Voici ce que j'ai fait de mon côté et ça fonctionne enfin ! et c'est grâce à toi qui m'a mis sur la bonne voie. Ça m'a permis d'apprendre et d'avancer dans l'apprentissage du PHP.

                            Donc un grand merci pour avoir pris de ton temps pour moi un dimanche.
                            Bien cordialement.

                            <?php
                            // information pour la connection à la bdd
                            $host = 'xxxxxxxxxx';
                            $user = 'xxxxxxxxxx';
                            $pass = 'xxxxxxxxxx';
                            $db   = 'xxxxxxxxxx';
                            
                            // connection à la bdd
                            $link = mysql_connect ($host,$user,$pass) or die ('Erreur : '.mysql_error() );
                            mysql_select_db($db) or die ('Erreur :'.mysql_error());
                            
                            
                            // requête SQL qui compte le nombre total d'enregistrement de la table
                            $select = ("select * from diagnostic ORDER BY date_crea DESC");
                            $result = mysql_query($select,$link) or die ('Erreur : '.mysql_error() );
                            $total = mysql_num_rows($result);
                            ?>
                            
                            <td align="center">
                            <?php
                            // si on a récupéré un résultat on l'affiche.
                            if($total) {
                                // debut du tableau
                                echo '<table bgcolor="#FFFFFF">'."\n";
                                    // première ligne on affiche les titres 
                                    echo '<tr>';
                                    echo '<td align="center" bgcolor="#669999"><b>Diagnostic</b></td>';
                            		echo '<td align="center" bgcolor="#669999"><b>Date d\'échéance</b></td>';
                            		echo '</tr>'."\n";
                                
                            $timestamp_today=mktime()+(86400*0); //ça on peut le sortir de la boucle pour éviter le calcul à chaque tour
                            
                            
                            // lecture et affichage des résultats.   
                                while($data = mysql_fetch_array($result)) {
                            
                            // formatage de la date + récupération infos diverses
                            	
                            $objDate_ech = New DateTime($data["date_echeance"]); 
                                
                            
                            $arrayDate_ech = explode('-',$data["date_echeance"]); 
                            $timestamp_Date_ech = date("d/m/Y",mktime(0,0,0,$arrayDate_ech[1],$arrayDate_ech[2],$arrayDate_ech[0])+(86400*7));
                             
                            if(date("d/m/Y") >= $timestamp_Date_ech) {
                                    echo '<td align="center" bgcolor="#FF0000">'.$data["designation_diag"].'</td>';
                            }elseif(date("d/m/Y") == $date_echeance = $objDate_ech->format("d/m/Y")) {
                                    echo '<td align="center" bgcolor="#FF9900">'.$data["designation_diag"].'</td>';
                            }else {
                                    echo '<td align="center" bgcolor="#CCCCCC">'.$data["designation_diag"].'</td>';
                            }
                            
                            		echo '<td align="center" bgcolor="#CCCCCC">'.$date_echeance = $objDate_ech->format('d/m/Y').'</td>';
                            
                            		echo '</tr>'."\n";
                                }
                                echo '</table>'."\n";
                                // fin du tableau.
                            }
                            
                            // on libère le résultat
                            mysql_free_result($result);
                            
                            // DECONNEXION MYSQL
                            close();
                            ?>
                                  </p>
                              </table>
                            </body>
                            </html> 
                            <?php