如果日期是今天

时间:2017-11-16 13:03:10

标签: php mysql

<?php
   $null=0;
   $sql = 'SELECT cl.id, cl.leaving_date, rn.id as rnid, rn.name as room_number
   FROM CLIENTS as cl
   JOIN room_number as rn ON cl.room_number_id=rn.id
   ORDER BY rn.name
   ';
   $strm = $DBcon ->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
   $strm -> execute(array(':id' => $null));
   $rows = $strm->fetchALL(PDO::FETCH_ASSOC);
   ?>
   <main>
     <?php
     $SQL = "SELECT name FROM cities";
     $strm = $DBcon->prepare($SQL);
     $strm->execute();
     $city = $strm->fetchAll();
     ?>
     <table>
       <tr>
         <th>Kambario numeris</th>
         <th>Atsilaisvins</th>
       </tr>
       <?php
       foreach ($rows as $row) {
         echo '<tr>
         <td>'.$row['room_number'].'</td>
         <td>'.$row['leaving_date'].'</td>
         </tr>';}
         ?>

数据库有2个表clientsroom_number。页面显示房间号和客户离开日期。如何检查日期是否为今天然后回显"room is free"

1 个答案:

答案 0 :(得分:0)

您可以使用CASE检查日期是否与curdate()

匹配
SELECT cl.id, cl.leaving_date,  
 case when date(cl.leaving_date)  = date(curdate() )
         then 'Room is free' else 'Room Occuped' 
 END free_or_not,
      rn.id as rnid, rn.name as room_number
 FROM CLIENTS as cl
 JOIN room_number as rn ON cl.room_number_id=rn.id
 ORDER BY rn.name

并显示正确的回声

  foreach ($rows as $row) {
     echo '<tr>
     <td>'.$row['room_number'].'</td>
     <td>'.$row['free_or_not'].'</td>
     </tr>';}
     ?>