-2

I am taking the input in M-d-Y format from calender but I want to convert it into Y-m-d when it store in the database. What method can I use?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Fahad Munir
  • 31
  • 2
  • 7

4 Answers4

-1

Try this:

$date="05-25-2014";
print(date("y-m-d",strtotime($date)));
Sharma Vikram
  • 2,440
  • 6
  • 23
  • 46
  • 1
    Doing this giving me this error: Undefined index: 70-01-01 in D:\xampp\htdocs\my bus ticket reservation system\myticket.php on line 84 – Fahad Munir Dec 02 '14 at 12:04
-1

You can try this. strtotime is your friend.

echo date("Y-m-d", strtotime('Dec-02-2014'));

or mysql DATE_FORMAT function, but mysql stores dates in yyyy-mm-dd format.

vaso123
  • 12,347
  • 4
  • 34
  • 64
-1

I think it better to do it in Mysql itself. When we have so many function. Like below:

SELECT STR_TO_DATE('Jan-31-2014', '%b-%d-%Y');

You can use it for insert query like below:

Insert into tableName `date` = STR_TO_DATE(InputVal, '%b-%d-%Y');

For more info:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
-2

use the date() and strtotime() like:

 $var = date('Y-m-d', strtotime($timestring))

Converting string to Date and DateTime

copy from: https://stackoverflow.com/a/6239010/2478144

Make not that there is a difference between using forward slash / and hyphen - in the strtotime function. to quote from php.net

Community
  • 1
  • 1
Benjamin Karlog
  • 330
  • 1
  • 6
  • 22