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)
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)
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');
strtotime
is a wonderful function. It works when you provide YYYY-MM-DD format but not with YY-MM-DD.
EDIT: As said in comments, you have to "turn the string into a valid ISO8601 Notations format by prepending '20' - this is understood by strtotime as specified in the docs "
This will work for the next 85 years with 64 bits systems (Until 2038 on 32 bit systems)
$file = '150220';
$newDate = date("Y-m-d", strtotime("20$file"));
You have to be careful with strtotime because it returns false on error which will make date
used today's date.
EDIT: if you want to -1 : first, this code works and it explains why op's code does not work, then consider using the "Add comment" button. Thanks.