Format Date

I'm reading values like this "13/08/2011 14:30" in from a CSV file and wondering if there was an easy way to convert them to "2011-08-13 14:30" for example so they can easily be inserted into a datetime column in mysql.


PeterErhardAsked:
Who is Participating?
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.

Ray PaseurCommented:
Usually strtotime() and date() play well together.  Let me see if I can give you a complete tested solution.  Back in a moment...
Ray PaseurCommented:
See http://www.laprbass.com/RAY_temp_petererhard.php
String manipulation appears the way to go.  HTH, ~Ray
<?php // RAY_temp_petererhard.php
error_reporting(E_ALL);

date_default_timezone_set('America/Chicago');

// TEST DATA FROM THE POST AT EE
$csv_date = "13/08/2011 14:30";

// UNFORTUNATELY DOES NOT WORK
$ts = strtotime($csv_date);
$iso_date = date('c', $ts);
echo "<br/>$csv_date == $iso_date";

// ANOTHER TRY
function re_date($csv_date)
{
    $arr = explode(' ', $csv_date);
    $new = explode('/', $arr[0]);
    $out = $new[2] . '-' . $new[1] . '-' . $new[0] . ' ' . $arr[1];
    return $out;
}
echo "<br/>$csv_date == " . re_date($csv_date);

Open in new window

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
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

PeterErhardAuthor Commented:
Thank you very much :)
Ray PaseurCommented:
Thanks for the points - it's a great question. ~Ray
nemws1Database AdministratorCommented:
Although Ray's answer is excellent and correct (as usual!), I just wanted to provide the in-MySQL way of doing this as well, namely with the STR_TO_DATE function:
INSERT INTO my_table (id, name, csv_date)
VALUES (NULL, "nemws1", STR_TO_DATE("13/08/2011 14:30", "%d/%m/%Y %H:%i"))
;

Open in new window

Of course, you can replace the static values in this with placeholders like usual.

More info here:
  http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date

And you need to read the manual page entry for DATE_FORMAT as well:
  http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format
PeterErhardAuthor Commented:
Ahh, that's awesome nemws1. Thanks for that, I think I'll go with your method, a lot easier.
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.