Reputation: 85
Previously I wanted to get time slots between $starttime
and $endtime
, divided by $duration
value.
We managed to get it working with this code :
$starttime = '9:00'; // your start time
$endtime = '21:00'; // End time
$duration = '30'; // split by 30 mins
$array_of_time = array ();
$start_time = strtotime ($starttime); //change to strtotime
$end_time = strtotime ($endtime); //change to strtotime
$add_mins = $duration * 60;
while ($start_time <= $end_time) // loop between time
{
$array_of_time[] = date ("h:i", $start_time);
$start_time += $add_mins; // to check endtie=me
}
$new_array_of_time = array ();
for($i = 0; $i < count($array_of_time) - 1; $i++)
{
$new_array_of_time[] = '' . $array_of_time[$i] . ' - ' . $array_of_time[$i + 1];
}
Now the issue is I don't want to show the timeslots that match dates in my appointments table.
To get the Appointments matching that date , i did :
$appointments = Appointment::where('user_id', $user->id)->where('appointment_datetime', $date->toDateString() )->get();
Now this returns me all appointments, ... in each appointment we have a column appointment_start and appointment_end , witch are formated in TIME .
Next step would be to check all appointment for matching timeslots ... and this is where I am stuck. Thank you for your help.
Upvotes: 4
Views: 679
Reputation: 596
Just replace the code with this one:
$starttime = '9:00'; // your start time
$endtime = '21:00'; // End time
$duration = '30'; // split by 30 mins
$array_of_time = array ();
$start_time = strtotime ($starttime); //change to strtotime
$end_time = strtotime ($endtime); //change to strtotime
$add_mins = $duration * 60; // seconds
while ($start_time <= $end_time) // loop between time
{
$array_of_time[] = date ("h:i", $start_time);
$start_time += $add_mins; // to check endtie=me
}
// Here I am getting the indexes of the time slot which has appointment
$indexes_to_be_skipped = array();
foreach($appointments as $appointment) {
for($i=0;$i<count($array_of_time); $i++) {
if($array_of_time[$i] == date ("h:i", strtotime($appointment['appointment_time_start']))) {
$indexes_to_be_skipped[$i] = $i;
}
}
}
$new_array_of_time = array ();
for($i = 0; $i < count($array_of_time) - 1; $i++)
{
$new_array_of_time[] = '' . $array_of_time[$i] . ' - ' . $array_of_time[$i + 1];
// check if current time slot has already appointment
if(isset($indexes_to_be_skipped[$i])) {
// then remove it
unset($new_array_of_time[$i]);
}
}
// resetting index
$narray_of_time = $new_array_of_time;
$new_array_of_time = array();
foreach($narray_of_time as $item) {
$new_array_of_time[] = $item;
}
I hope it helps!
Upvotes: 3
Reputation: 1822
This may help
<?php
$date = \Carbon\Carbon::now(); // the date you need to check
$starttime = \Carbon\Carbon::create($date->year, $date->month, $date->day, 9,0); // your start time
$endtime = \Carbon\Carbon::create($date->year, $date->month, $date->day, 21,0); // end time
$add_mins = 60; // minutes to add to each meeting
$array_of_time = [];
$current_time = $starttime->copy();
while($current_time->isBefore($endtime)){
// check if we have record between current and current + $add_mins
// I suppose appointment_datetime is a datetime field in database
if(!Appointment::where('user_id', $user->id)->whereBetween('appointment_datetime', [$current_time, $current_time->copy()->addMinute($add_mins)])->count()){
$array_of_time[] = $current_time->format('H:i') . ' - ' . $current_time->copy()->addMinute($add_mins)->format('H:i');
}
$current_time->addMinute($add_mins);
}
Upvotes: 0