Skip to main content

Bind Date Values During Import

Date formats in your import file often differ from the format stored in the Deepser database. This example shows how to parse an incoming date value and reformat it so that it is saved correctly.

In the code below, the ACTIVATION DATE column is converted from d/m/Y format to the database-accepted Y-m-d format and assigned to the Activation Date model field.


/* the conversion is surrounded with a try-catch to avoid throwing exceptions */
try{
/* a DateTime object is created from the format used in the import file */
$date = DateTime::createFromFormat('d/m/Y', $value);
/* the field 'cust_activation_date' is set with the created date formatted in the Database
accepted format */
$model->setCustActivationDate($date->format('Y-m-d'));
}
catch(Exception $exxx){
/* any exception is printed to the import_device.log log file, if the file doesn't exist
it will be created */
Deep::log($exxx,null,'import_device.log');
Deep::log('value: '.$value, null, 'import_device.log');
Deep::log('date: '.$date, null, 'import_device.log');
}

tip

Always wrap date conversions in a try-catch block. If a row contains an invalid date, the exception is logged and the import continues processing the remaining rows.