0

I have a page that's returning data, on the ship date some values are null but return "as 01-01-1970". My goal is to return a blank field if the value is NUll on the DB. I wrote this statement, but it doesn't seem to work correctly. Trying to figure out how to solve this, I'm new to php any help is appreciated

<td><?php echo $row['status_details']; ?></td>
<td><?php echo $row['order_date']; ?></td>-->

<?php
$ship_date = $row['ship_date'];;
$ship_date = date("m-d-Y", strtotime($ship_date));

  if($ship_date == 'NULL') {
    $ship_date = "" 
  } else {
      <td><?php echo $ship_date; ?></td> }
      ?>

<td><?php echo $row['shipped_qty']; ?></td>
<td><?php echo $row['shipping_carrier']; ?></td>
<td><?php echo $row['tracking_number'];?></td>

  • `'NULL'` is a text literal. https://www.php.net/manual/en/language.types.null.php And https://www.php.net/manual/en/function.is-null.php exists. – misorude Aug 27 '19 at 12:43
  • But `if($ship_date)` should probably do it already. (I doubt you’ll need to differentiate this case from an actual 0 value.) – misorude Aug 27 '19 at 12:44
  • Only change the condition to `if(empty($ship_date)){` because empty function can check if the value is empty or null – Vijay Dohare Aug 27 '19 at 13:06

1 Answers1

0

Don't print the $ship_date in else part but print like others td value.

<td><?php echo $row['status_details']; ?></td>
<td><?php echo $row['order_date']; ?></td>

<?php
$ship_date = strtotime($row['ship_date']);
$ship_date = $ship_date == NULL ? "" : date("m-d-Y", $ship_date);
?>
<td><?php echo $ship_date; ?></td>
<td><?php echo $row['shipped_qty']; ?></td>
<td><?php echo $row['shipping_carrier']; ?></td>
<td><?php echo $row['tracking_number'];?></td>

Also the 'NULL' might be NULL. Condition will be true if you use 'NULL' because that is a string not NULL type.

MH2K9
  • 11,951
  • 7
  • 32
  • 49
  • Thank You for your help, I tried this and it still returns a date of 01-01-1970 – The Emperors New Brew Aug 27 '19 at 13:08
  • If you pass invalid timestamp to `data()`, by default it returns `1st January, 1970`. So you have to check the timestamp before formatting date. See the updated answer. – MH2K9 Aug 28 '19 at 05:06