Search code examples
phpxmldatetimedate-conversion

Change date format to import into Mysql


I need help changing this date format:

I get it in a XML format that looks like this: 15 08 14. I am trying to import it to MySQL with PHP and my code looks like this:

$Date = date_format(date_create_from_format('%d %m %Y', $record->Date), 'Y-m-d');

But it doesn't work I keep getting errors:

Warning: date_format() expects parameter 1 to be DateTime


Solution

  • Try this.

    date('Y-m-d', strtotime(str_replace(' ', ':', $record->Date)));
    

    EDIT
    The problem was that the spaces in the original date format, prevented PHP from recognizing any date format related.

    However, this might not be the most elegant answer, if someone else has a better way of detecting your date format (the one retrieved from the XML doc) that suggestion should be used.