1

I am returning some search results after form submit. All is working fine, until I get to a field that is SMALLDATETIME and allows NULL. At least one of the returned rows has the field as NULL. As you can see in the code below, when it is not NULL I am converting it from Date to String no problem, but i get an error Message for the rows with NULL.

Any ideas?

Code:

$search_results = sqlsrv_query($database_connection, $search_results_sql, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

if($search_results){
    $returned_rows = sqlsrv_has_rows($search_results);
    if($returned_rows === true){
        getAssetSearchData($search_results);
    }
}
function getAssetSearchData($search_results){

while($search_results_option = sqlsrv_fetch_object($search_results)){
    echo "
<tr>
        <td class='col45'>".date_format($search_results_option->HardwareAssetLastUpdateTime,"d/m/Y H:i")."</td>
        <td class='col46'>".$search_results_option->HardwareAssetLastUpdatedByName."</td>
        <td class='col47'>".date_format($search_results_option->HardwareAssetLastDiscoveryScanDate,"d/m/Y H:i")."</td></tr>";
    }
}

Error Message:

Warning: date_format() expects parameter 1 to be DateTimeInterface, null given in

2
  • you could add a check if($search_results_option->HardwareAssetLastUpdateTime === NULL). Maybe it could help you when you add the "on update current_timestamp" Flag to the LastUpdateTime in your DB Commented Nov 18, 2016 at 8:27
  • date_format doesn't work with null date values. You should have to manage this case Commented Nov 18, 2016 at 8:28

2 Answers 2

3
<td>".(
   is_null($search_results_option->HardwareAssetLastDiscoveryScanDate) ? '' : 
       date_format($search_results_option->HardwareAssetLastDiscoveryScanDate,
        "d/m/Y H:i")
    )
 ."</td>";
Sign up to request clarification or add additional context in comments.

Comments

1

Try this,

<td class='col45'>".date_format(new DateTime( $search_results_option->HardwareAssetLastUpdateTime ),"d/m/Y H:i")."</td>
<td class='col46'>".$search_results_option->HardwareAssetLastUpdatedByName."</td>
<td class='col47'>".date_format(new DateTime( $search_results_option->HardwareAssetLastDiscoveryScanDate ),"d/m/Y H:i")."</td></tr>";

date_format() need the date to be in DateTime format when supplied, you are passing a string.

The other thing may be that $search_results_option is empty

For NULL values:

( $search_results_option->HardwareAssetLastDiscoveryScanDate != null ? date_format(new DateTime( $search_results_option->HardwareAssetLastDiscoveryScanDate ),"d/m/Y H:i") : '' )

This will check for a null value, if it is not null it will display the formatted datetime, els eit will display nothing

1 Comment

$search_results_option is empty for HardwareAssetLastDiscoveryScanDate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.