Search code examples
phpmysqldatedate-conversion

PHP Date Conversion YYMMDD to yyyy-mm-dd


Hi I am struggling with converting a string:

$file = '150220';

into a mysql date format.

I have tried this

$newDate = date("Y-m-d", strtotime($file));

But it returns the following 2015-02-24 (24 being today's date)


Solution

  • See: http://php.net/manual/en/datetime.createfromformat.php

    So you can use this:

    $dateTime = DateTime::createFromFormat('ymd', $file);
    $newDate = $dateTime->format('Y-m-d');