12

I have a PHP function that gets passed a date in the format MM/DD/YYYY I need to then convert this so that it can be added to a MySQL field that is of type date

How would I go about doing this in PHP?

DevWithZachary
  • 3,545
  • 11
  • 49
  • 101

4 Answers4

29
$newvalue = date('Y-m-d', strtotime($originalvalue));
Mattt
  • 1,780
  • 14
  • 15
7

MySQL displays the DATE type as 'YYYY-MM-DD', so you could do something like:

date("Y-m-d",strtotime("10/18/2013"));

Saty
  • 22,443
  • 7
  • 33
  • 51
PaulV
  • 81
  • 1
4

My variant:

  $mysql_date = date('Y-m-d', strtotime(str_replace('/','-', $value)));
CreatoR
  • 1,654
  • 10
  • 14
1
$date = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$3-$1-$2', $date)
dewil
  • 19
  • 4