I have a date ("mm/dd/yyyy") and I want to convert it to a MySQL DATE data type (like yyyy-mm-dd)
How do I do it with PHP?
Take a look at mysql function str_to_date()
example
select str_to_date('10/30/2010','%m/%d/%Y') -- 2010-10-30
Lots of ways to do it. I like always converting my dates to a timestamp cause I find it easiest to then do what I want.
In which case:
<?php
echo date( "Y-m-d",strtotime("09/02/1988"));
?>
03/04/2011? Is that April 3rd or March 4th? strtotime is handy but it's NOT a magic bullet. Safer to use date_create_from_format() when you know what the format is in advance.If your date is in $your_date, then:
$mysql_date = date('Y-m-d', strtotime($your_date));
DATE type? This is actually more a string than real time format (like TIMESTAMP where the value is stored in a Unix epoch).$date = '12/25/2011';
$parts = explode('/', $date);
$sql_date = $parts[2] . '-' . $parts[0] . '-' . $parts[1];
01/02/2011 and you have no idea what each field represents, then there's nothing that can help.function date2mysql($date) {
list($month, $day, $year) = explode('/', $date);
$timestamp = mktime(0, 0, 0, $month, $day, $year);
return date("Y-mm-d",$timestamp);
}
see the date manual for the format