I have a ScheduleController that gets the start_time and end_time from the schedules table.
ScheduleController
$schedules = Schedule::select('start_time', 'end_time')
->where('weekday', $request->input('weekday'))
->get()
->toArray();
$collection = collect($schedules)->map(function ($time) {
return [date("g:i", strtotime($time['start_time'])), date("g:i", strtotime($time['end_time']))];
})->flatten();
$data = $collection->values()->all();
// the $data above produce something like this
['6:30', '7:30'],
['11:30', '12:30']
I'm getting the array response from my ScheduleController, and in my blade file I have a BootStrap DateTimePicker, this DateTimePicker will use the array response in disabledTimeIntervals.
The syntax of disabledTimeIntervals looks like the code below, the hour and minutes are hard coded here:
// example
disabledTimeIntervals: [
[ moment({ hour: 17, minute: 30 }), moment({ hour: 18, minute: 30 }) ]
],
The question is, How can I format the array response from my ScheduleController to be like this:
// FROM
['6:30', '7:30'],
['11:30', '12:30']
// TO
[ moment({ hour: 6, minute: 30 }), moment({ hour: 7, minute: 30 }) ],
[ moment({ hour: 11, minute: 30 }), moment({ hour: 12, minute: 30 }) ]
EDIT
I tried Mtdt 's answer, but it's not disabling the hour and minutes if I put multiple arrays, but in the single array it works, See the example below, I manually add it to the code.
const stringToDate = (dateString) => {
const [hours, minutes] = dateString.split(':');
return moment().hours(hours).minutes(minutes);
}
const formatDates = datesToFormat => datesToFormat.map(dateString => {
if (Array.isArray(dateString)) {
return dateString.map(s => stringToDate(s));
} else {
return stringToDate(dateString);
}
})
let customOptions = {
format: 'hh:mm A',
disabledTimeIntervals: [
// not working in multiple array
formatDates([ ['6:30', '7:30'], ['11:30', '12:30'] ])
// this one works if single array and not enclosed with []
formatDates(['6:30', '7:30'])
],
}
$('.timepicker').datetimepicker(customOptions)
Maybe it's not formatted like below?
disabledTimeIntervals: [
[moment({ hour:6, minute:30 }), moment({ hour:7, minute:30 })],
[moment({ hour:11, minute:30 }), moment({ hour:12, minute:30 })]
],
This is the result when I console.log(formatDates([['6:30', '7:30'], ['11:30', '12:30']]))
EDIT #2
I tried doing it the PHP way, but there are still some problems I'm facing.
My problem now is removing the quotes (") in the array result and the extra comma (,) at the end. Currently, I have str_replace('"', "", 'string-here') but it's not removing the quotes.
My code
$user = User::find(1);
$schedules = Schedule::select('start_time', 'end_time')
->whereBelongsTo($user)
->where('weekday', 1)
->get()
->toArray();
$collection = collect($schedules)->map(function ($time) {
return [date("g:i", strtotime($time['start_time'])), date("g:i", strtotime($time['end_time']))];
});
$data = $collection->values()->all();
// $data produces this result
// [
// [
// "6:45",
// "7:45",
// ],
// [
// "10:30",
// "11:30",
// ],
// ]
$carrier = [];
foreach ($data as $key => $a) {
$detail1 = preg_split('/:/i', $a[0]);
$detail2 = preg_split('/:/i', $a[1]);
array_push($carrier, array(str_replace('"', "", 'moment({ hour:'.$detail1[0].', minute:'.$detail1[1].' }), moment({ hour:'.$detail2[0].', minute:'.$detail2[1].' })')));
}
return $carrier;
// currently this is what I got as the result when I return $carrier
[
[
"moment({ hour:6, minute:45 }), moment({ hour:7, minute:45 })",
],
[
"moment({ hour:10, minute:30 }), moment({ hour:11, minute:30 })",
],
]
// and I want to remove the quotes " and the extra comma , at the end of the string
// so this is the final form
[
[
moment({ hour:6, minute:45 }), moment({ hour:7, minute:45 })
],
[
moment({ hour:10, minute:30 }), moment({ hour:11, minute:30 })
],
]

momented on the frontend?momented it on frontend? yes['6:30']tomoment({ hour: 6, minute: 30 })