AdRock
AdRock

Reputation: 3096

php get time intervals between 2 times

I am trying to create a booking form where a user can select a booking time between 2 given times in 5 minute intervals. For example I want time slots between 10am and 12pm which would give me about 20 time slots.

When the user goes to select a slot, the earliest slot should be at least 15 mins ahead of the current time but the user can select a slot and hour or more if desired.

I found some code on SO (can't remember where) and I've edited it for my needs and it works if the current time is within the start and end time but if the current time is an hour before the earliest time, it doesn't create the time slots.

I know why it does it but i don't know how to fix it. It has to do with the while condition.

I would like to be able to book a slot hours before the first available slot if that is possible.

    $timenow = time();

    $start_time = strtotime('+15 minutes', $timenow);
    // round to next 15 minutes (15 * 60 seconds)
    $start_time = ceil($start_time / (5 * 60)) * (5 * 60);
    //set the start times
    $opentime = strtotime('10:00');
    $closetime = strtotime('11:55');

    // get a list of prebooked slots from database
    $time_slots = $this->countStartTimes();

    $available_slots = array();

    while($start_time <= $closetime && $start_time >= $opentime) {

        $key = date('H:i', $start_time);

        if(array_key_exists($key, $time_slots)) {
            if($time_slots[$key] == SLOTS) {
                $available_slots[] = 'FULL'; 
                break;
            }
        }

        $available_slots[] = date('H:i', $start_time);

        $start_time = strtotime('+5 minutes', $start_time);
    }

Upvotes: 0

Views: 2270

Answers (2)

AdRock
AdRock

Reputation: 3096

I managed to get it working using Datetime()

$timenow = new DateTime(date('H:i'));
$timenow->add(new DateInterval('PT15M'));

$start = new DateTime('11:00');
$end = new DateTime('14:00');
$interval = new DateInterval('PT5M');

$time_slots = $this->countStartTimes();

$available_slots = array();

$period = new DatePeriod($start, $interval, $end);

foreach($period as $time) {

    $timeslot = $time->format('H:i');

    if ($timenow > $time) {
        continue;
    }            

    if(array_key_exists($timeslot, $time_slots)) {
        if($time_slots[$timeslot] == SLOTS) {
            $available_slots[] = array('key' => $timeslot, 'value' => 'FULL'); 
            continue;
        }
    }

    $available_slots[] = array('key' => $timeslot, 'value' => $timeslot);
}

Upvotes: 3

keerti Sawlani
keerti Sawlani

Reputation: 233

Carbon has all of the functions inherited from the base DateTime class. This approach allows you to access the base functionality if you see anything missing in Carbon but is there in DateTime.

 // Carbon::diffInYears(Carbon $dt = null, $abs = true)

    echo Carbon::now('America/Vancouver')->diffInSeconds(Carbon::now('Europe/London')); // 0

    $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');
    $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver');
    echo $dtOttawa->diffInHours($dtVancouver);                             // 3

    echo $dtOttawa->diffInHours($dtVancouver, false);                      // 3
    echo $dtVancouver->diffInHours($dtOttawa, false); 

Use carbon class for this it really help you

Upvotes: 1

Related Questions