user5621153
user5621153

Reputation:

How to empty check a date field with 0000-00-00?

$array = Array(
     [FetchedData_I] => 1
     [EM_Account_V] => Test 2
     [EM_Date_D] => 2015-11-20
     [EM_Time_T] => 00:00:00
     [Channel_V] => 
     [OD_Online_Listing_ID_V] => 
     [OD_Item_Name_V] => Item Namesdsd
     [OD_Color_V] => 
     [OD_Condition_V] => 
     [OD_Qty_V] => 1
     [OD_Order_Date_D] => 0000-00-00
     [OD_Order_Time_T] => 00:00:00
     [OD_Ship_By_Date_D] => 0000-00-00
     [Comments_Internal_B] => 
 );

The above is the mysql data returned by a query and the scenario is to ignore the empty data and process the other data.

I'm currently using:

foreach($array as $value) {
   if($value!='') {
      //Process the other
   }
}

In the above case, how to check for the empty data of time and date like EM_Time_T, OD_Order_Date_D etc to see if they are empty?

Upvotes: 4

Views: 7095

Answers (1)

rybo111
rybo111

Reputation: 12588

Don't try to handle 0000-00-00 in PHP. Instead, fix the problem with your table.

In phpMyAdmin, edit the structure of the table and tick the NULL box for each date field. This means if the field is not set when inserted, it will be NULL instead.

Then, run the following SQL in phpMyAdmin, but change table_name and field_name as appropriate:

UPDATE table_name SET field_name = NULL WHERE field_name = '0000-00-00';

To answer your PHP foreach question, your code should look like this:

foreach($array as $key => $value) {
   if($value!='') {
      //Process the other
   }
}

Upvotes: 10

Related Questions