简体   繁体   中英

Codeigniter merge form fields into database

I have two form fields on my view page, a date input (with a date selector popup) and a time input.

When passing these values into the model, is it possible to merge them together so they will both go into a single database field with a type of Timestamp? eg 12/12/2012 + 14:20 and saved to the db as 2012/12/12 14:20:00

Thanks in advance

If you just want to join them together, and you're fine with the combined field being a string, you could just concatenate.

$date = $this->input->post('date');
$time = $this->input->post('time');

$timestamp = "$date "."$time";

You would also need to create a field in your DB model for this new timestamp field.

You should do some sort of validation of the date as well to be sure if fits your db format

something like below:

$date = new DateTime($this->input->post("date");
$time = new DateTime($this->input->post("time");

$datetime = $date->format("Y-m-d") . $time->format("H:i:s");

Then use the $datetime variable in your model to be inserted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM