Maii Maii - 2 years ago 61
SQL Question

insert date and time separately into mysql

How can I store date (MM/DD/YYYY) and time (hour/minute AM) into mysql? and how I set the datatype of date and time field in mysql?

This is my attempt but the data are not uploaded to mysql table.

Here's the controller :

'pickup_date' => date ("Y-m-d",strtotime($pickup_date)),
'pickup_time' => date ("h:i A",strtotime($pickup_time))

Here's the input form :

<input data-format="MM/dd/yyyy" type="text" class="form-control" name="pickup_date"></input>
<input data-format="HH:mm PP" type="text" class="form-control" name="pickup_time"></input>

Answer Source

Have a column for Date and Time (pickup_date). Data type could be DATETIME.

Then, when inserting date to this column using your form, you can format the submitted POST data using date() and strtotime().

$date = date('Y-m-d H:i:s', strtotime($_POST['pickup_date'].' '$_POST['pickup_time']));
/*** YYYY-MM-DD HH:MM:SS ***/

Then insert (assuming you are using CodeIgniter):

$data = array(
    'pickup_date' => $date

You can check here for more date format.

For your HTML form, you can use the input date and time (both works for Edge, Chrome, Safari and Opera):

<input type="date" class="form-control" name="pickup_date">
<input type="time" class="form-control" name="pickup_time">

<input> does not need a closing </input>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download