Reputation: 18948
I'm having the date in this format in a Excel worksheet 03-Dec-10. So its not compatible while inserting it into the database. How to convert the date to the acceptable format?
Upvotes: 0
Views: 4255
Reputation: 107716
If you're doing this from Excel itself, you can put this formula into another column
=TEXT(A2, "YYYYmmdd")
Then copy down. This produces a compatible 8-digit date.
Upvotes: 0
Reputation: 90861
While Jon's answer is correct, here is another option:
$input = '03-Dec-10';
$date = date('Ymd', strtotime($input));
Upvotes: 2
Reputation: 1744
For a more general approach, you can always dump your current format to a string, like how you have it, and use string operations to substring and reorganize. I know for a fact that MySQL accepts string values for DATETIME
fields.
$day = substr($input, 0, 2);
$month = substr($input, 2, 3);
switch($month){
case "Jan":
$month = "01";
break;
...
}
Upvotes: 0
Reputation: 437376
$input = '03-Dec-10';
$date = DateTime::createFromFormat('d-M-y', $input);
echo $date->format('Ymd'); // or possibly 'Y-m-d'
This will output 20101203
, which is presumably what you want. If it's not exactly what you are after, have a look here.
You can also do the reverse:
$input = '20101203';
$date = DateTime::createFromFormat('Ymd', $input);
echo $date->format('d-M-y');
Upvotes: 3