0

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 2

1

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];
Sign up to request clarification or add additional context in comments.

5 Comments

Are you still around?
yes, how can i help?@itsmejoe
How about doing the inverse on edit() ? I'd want the $colValueToBeStored to be exploded again to $time_to and $time_from. TIA
i added the reverse part to answer. @itsmejoe
Woah Big thanks,
1

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
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.