Key K Key K - 11 months ago 43
PHP Question

How to highlight each cell in table that returns '0000-00-00', empty or invalid date?

Im trying to highlight cells that are returning '0000-00-00', empty or invalid date from table but I cant find any solution.Please see the code below

<table class="main_table_med">
<tr class="med_tr">
<th>Exam Location</th>
<th>Exam Date</th>
</tr>
<tr>
<?php
while($row = mysql_fetch_array($select)){
?>
<td id='ex_date' class="tb_dt"><?php echo $row['exam_date']?></td>
<td id='due' class="tb_dt"><?php echo $row['exam_due']?></td>
</tr>
<?php
}
?>
</table>

Answer Source

You tagged the question with javascript jquery but it's really should be PHP question, use PHP to check if it's valid date or not and show the result accordingly:

<?php
function validateDate($date, $formatExpected = 'Y-m-d H:i:s')
{
  $d = DateTime::createFromFormat($formatExpected, $date);
  return $d && $d->format($formatExpected) == $date
          ? $date
          : 'anyThing';
}
?>

<table class="main_table_med">
                    <tr class="med_tr">
                        <th>Exam Location</th>
                        <th>Exam Date</th>
                    </tr>
                        <tr>
                    <?php
                            while($row = mysql_fetch_array($select)){
                            ?>
                            <td id='ex_date' class="tb_dt"><?php echo validateDate($row['exam_date']) ?></td>
                            <td id='due' class="tb_dt"><?php echo $row['exam_due']?></td>
                        </tr>
                         <?php
                         }  
                        ?>  
                </table>