0

I tried asking this question earlier but I had a prior problem of properly obtaining textual month, day and year date from an input field in an HTML table.

Now that I have obtained this string data, in this case "Aug 25, 2014", I want to convert into date data that will eventually be inserted into a MySQL table.

PHP:

$todays_date = strtotime("today");

echo "<form name='timesubmit" . $time_cell_row . "' action='enter_time.php?action=timesubmit" .$time_cell_row . "' method='POST'>";  

echo "<table>"
echo "<th><input name='daycell1' type='text' value='$FormattedCurrDate' readonly/></th>";
echo "</table>";
echo "</form>";

/* Code in place to GET action from URL

*/

$date1 = $_POST['daycell1'];
          echo "Date from input field: " . $date1; // displays "Aug 25, 2014

How do I take the variable '$date1' and convert it into a date format so that it can be inserted into a MySQL table with a field of type "date"?

Jeff P.
  • 2,754
  • 5
  • 19
  • 41
  • just reformat it again (`date('Y-m-d', strtotime($date1))`) before inserting, use date function again, and please don't use `mysql_` functions anymore, use `mysqli_*` or `PDO` instead – Kevin Aug 26 '14 at 03:25

1 Answers1

2

Use date() and strtotime() to format your date to be inserted into your table. Then to display it use date function again to display it in a format you want.

<?php
$date1 =  $_POST['daycell1']; //I used Aug 25, 2014 in the DEMO.
$date1 = date("Y-m-d", strtotime($date1));
echo $date1; //echoes 2014-08-25 which will be inserted into your table
?>

DEMO

Mark Miller
  • 7,442
  • 2
  • 16
  • 22
John Robertson
  • 1,486
  • 13
  • 16