PHP Convert Dates

I have the following data set that I need to convert to yyyy-mm-dd

2015-May-1
2015-May-2
2015-May-3
2015-May-4
2015-May-5
2015-May-6
2015-May-7
2015-May-8
2015-May-9
2015-May-10
2015-May-11
2015-May-12
2015-May-13
2015-May-14
2015-May-15
2015-May-16
2015-May-17
2015-May-18
2015-May-19
2015-May-20
2015-May-21
2015-May-22
2015-May-23
2015-May-24
2015-May-25
2015-May-26
2015-May-27
2015-May-28
2015-May-29
2015-May-30
2015-May-31

Open in new window


I tried using strtotime on them but anything that was less than a two digit day seems to fail the conversion.
LVL 12
Nathan RileyFounderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Maidine FouadEngineerCommented:
Hello

The data you gave is already Formated to yyyy-mm-dd ^^ ?!

nonetheless if you want to convert from any format to any format, the createFromFormat Function is the optimal solution

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

$date = date_create_from_format('Y-M-j', '2009-Feb-15');
echo date_format($date, 'Y-m-d');

Open in new window


For 2009-Feb-15

It will Give as a result : 2009-02-15

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Raymond van den BedumCommented:
You can create a timestamp from a format like:

<?php 
$date = date_create_from_format('Y/M/j', '2015-May-1'); 
?> 

Open in new window


afterwords you can create your date:
<?php date('Y-m-d', $timestamp); ?>
Nathan RileyFounderAuthor Commented:
Hmm...while trying to use that code I receive a php error on the page:
 Catchable fatal error: Object of class DateTime could not be converted to string in
Raymond van den BedumCommented:
This should be the right implementation:

$date = date_create_from_format('Y-M-j', '2015-May-1');
echo date('Y-m-d', strtotime($date->date));

Open in new window

Ray PaseurCommented:
How to handle date and time issues in PHP:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Given the test data, I would just use str_replace('May', '5', $dataset);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.