0

I'm using the below code to take the date input from the user:

<p>Start Date: <input id="start" name="start_Date" type="date" /></p>

I'm processing this input through PHP script and inserting into a database as follows:

startDate = '{$f['start_Date']}'

This inserts the date in the database in yyyy-mm-dd. How to change this to mm/dd/yyyy format before inserting it into the database.

Alexander Paes
  • 101
  • 4
  • 13
  • `mm/dd/yyyy` is not mysql's date format. Do you mean you want to output as that? – chris85 Nov 23 '15 at 16:49
  • yes, I don't mind even if it is stored as a text(I'll change the column type to text from date in db if needed). I want it to be in mm/dd/yyyy format – Alexander Paes Nov 23 '15 at 16:53
  • 2
    You should store it as a date then modify its display on output. You are going to lose all date functions by making it text/string. – chris85 Nov 23 '15 at 16:55
  • Possible duplicate of [Convert from MySQL datetime to another format with PHP](http://stackoverflow.com/questions/136782/convert-from-mysql-datetime-to-another-format-with-php) – Taz Nov 23 '15 at 18:49

2 Answers2

0

mysql date format is yyyy-mm-dd,

so before inserting the code, use strtotime() and convert the date to mysql format.

echo date("Y-m-d",strtotime($_POST['start_Date']));

For more format's(All possible formats), see this

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
0

Create a datetime object out of your date, then format it as you wish.

http://php.net/manual/de/datetime.format.php

$myDate = new DateTime('2000-01-01');
echo $myDate->format('Y-m-d H:i:s');
user5542121
  • 1,051
  • 12
  • 28