Display a NULL date as blank string with Php

Let me preface this with the fact that I am not a programmer by any means.

I’ve created a page that has a monthly schedule. I also put together a back end with a form that allows the information to be updated.

I’ve inserted all the information into the database and I’ve assigned the date column to NULL.

How can I make it so that when I submit a NULL date it displays as blank instead of displaying the current date?

 <tr class="<?php $time = strtotime($row_Recordset1['31Date']); echo date("D", $time); ?>">
      <td><input type="text" name="31Date" style="width:75px;" value="<?php echo $row_Recordset1['31Date']; ?>" /></td>
      <td><input type="text" name="31MD1" value="<?php echo $row_Recordset1['31MD1']; ?>" /></td>
      <td><input type="text" name="31MD2" value="<?php echo $row_Recordset1['31MD2']; ?>" /></td>
      <td><input type="text" name="31CRNA1" value="<?php echo $row_Recordset1['31CRNA1']; ?>" /></td>
      <td><input type="text" name="31CRNA2" value="<?php echo $row_Recordset1['31CRNA2']; ?>" /></td>
    </tr>

If your database is returning a null value then you can do it as part of the query.

If you database is mysql then look at the coalesce() function.

If you cant get a grip of the sql query, maybe you are using some kind of orm or active record, then you’d need to do it at the PHP level. Messier.

I’ll take a guess its this one:

<?php echo isset($row_Recordset1[‘31Date’]) ? $row_Recordset1[‘31Date’] : “” ; ?>

Equivalent of :

if $row is set (ie not null) then echo the row else echo an empty string.

Using the shorthand ternary operator. (true) ? 'do this : 'do that ;

So you should be able to apply the same idea to the field giving you the problem.

You can use an “if / else” condition block to set a variable to display.

if (is_null($rowset['date'])) {
    $date = '';
} else {
    $date = date('D', strtotime($rowset['date']));
}

http://php.net/manual/en/control-structures.if.php