I am importing data from a CSV file and one column has a list of dates. Some of these dates are pre 1970. An example date would be 10/03/1956
I've got a problem in formatting these dates into a format that I can insert into MySQL.
if i use this
$date = DateTime::createFromFormat('d/m/Y', $col[8]);
I get this error message
Call to a member function format() on a non-object
but if I hard code a date like this, it works
$date = DateTime::createFromFormat('d/m/Y', '21/03/1966');
here is the complete code
$inputFileName = 'data.csv';
if (($handle = fopen($inputFileName, "r")) !== FALSE) {
fgetcsv($handle);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
for ($i = 0; $i < $num; $i++) {
$col[$i] = $data[$i];
}
try {
$date = DateTime::createFromFormat('d/m/Y', trim($col[8]));
}
catch (Exception $e) {
echo $e->getMessage();
exit(1);
}
echo $date->format("Y-m-d");
}
fclose($handle);
}