So I have 2 variable for storing the selected time of the user ('time_to' and 'time_from) with these sample data('7:30','8:00') How can I save these two into 1 column('c_time') so it would look like this('7:30-8:00')?
2 Answers
if i understand you correctly, you can create a column of string (varchar) type. and then create the data for your column like this :
$time_to = '7:30';
$time_from= '8:00';
$colValueToBeStored = "(".$time_to."-".$time_from.")";
then just put $colValueToBeStored inside your column.
and to reverse it:
$colValueToBeStored = "(7:30-8:00)";
$res = explode("-",str_replace([")","("],"",$colValueToBeStored));
$time_to = $res[0];
$time_from = $res[1];
5 Comments
itsme joe
Are you still around?
Mah Es
yes, how can i help?@itsmejoe
itsme joe
How about doing the inverse on edit() ? I'd want the $colValueToBeStored to be exploded again to $time_to and $time_from. TIA
Mah Es
i added the reverse part to answer. @itsmejoe
itsme joe
Woah Big thanks,
define your c_time column type as JSON, that way you can store multiple values, as it will be easier to retrieve as well. Like,
...
$cTime['time_to'] = "7:30";
$cTime['time_from'] = "8:00";
$cTimeJson = json_encode($cTime);
// save to db
...