ITit superpower
ITit superpower

Reputation: 526

Partion 20 min slot of time in php from start and end time

Ex ->

 Start time = 2015-10-21 09:00:00
 end time  = 2015-10-21 19:45:00

so as per my example - start time is 9 am to 7-45pm

so i want possible slot of 20 min. in php like

9:00 - 9:20 , 9-20 to 9-40 etc. but also for ex 12:00-12:20 , 2:40 to 2:60 booked then it will not included in this list.

I want to make 20 min slot. yes but it will also filter alr

Using the time difference in php i have start time and end time.

And then i want to check that how many 20 min slot possible from start and end time

=> it will also check if some time slot already booking then not consider it

IN SHOT 1 to 2 or 3-5 is break time then it will not consider. 

Upvotes: 2

Views: 5136

Answers (2)

Subin Thomas
Subin Thomas

Reputation: 1416

This will do what you need. But specific format should be kept for array elements, for easy calculations.

<?php
$start_time = '2015-10-21 09:00:00';  //start time as string
$end_time = '2015-10-21 19:45:00';  //end time as string
$booked = array('12:20-12:40','13:00-13:20');    //booked slots as arrays
$start = DateTime::createFromFormat('Y-m-d H:i:s',$start_time);  //create date time objects
$end = DateTime::createFromFormat('Y-m-d H:i:s',$end_time);  //create date time objects
$count = 0;  //number of slots
$out = array();   //array of slots 
for($i = $start; $i<$end;)  //for loop 
{
$avoid = false;   //booked slot?
$time1 = $i->format('H:i');   //take hour and minute
$i->modify("+20 minutes");      //add 20 minutes
$time2 = $i->format('H:i');     //take hour and minute
$slot = $time1."-".$time2;      //create a format 12:40-13:00 etc
    for($k=0;$k<sizeof($booked);$k++)  //if booked hour
    {
    if($booked[$k] == $slot)  //check
    $avoid = true;   //yes. booked
    }
if(!$avoid && $i<$end)  //if not booked and less than end time
{
$count++;           //add count
$slots = ['start'=>$time1, 'stop'=>$time2];         //add count
array_push($out,$slots); //add slot to array
}
}
var_dump($out);   //array out
echo $count ." of slots available";

If you use booked datetime as array, use below code.

<?php
$start_time = '2015-10-21 09:00:00';  //start time as string
$end_time = '2015-10-21 19:45:00';  //end time as string
$booked = ['2015-10-21 12:20:00','2015-10-21 12:40:00', '2015-10-21 13:00:00','2015-10-21 13:20:00'];
     //booked slots as arrays
$start = DateTime::createFromFormat('Y-m-d H:i:s',$start_time);  //create date time objects
$end = DateTime::createFromFormat('Y-m-d H:i:s',$end_time);  //create date time objects
$time1 = $start;
$count = 0;  //number of slots
$out = array();   //array of slots 
for($i = $start; $i<$end;)  //for loop 
{
$avoid = false; 
$t1 = date_timestamp_get($i);
$t2 = $t1+(20*60);

    for($k=0;$k<sizeof($booked);$k+=2)  //if booked hour
    {
    $st = DateTime::createFromFormat('Y-m-d H:i:s',$booked[$k]);
    $en = DateTime::createFromFormat('Y-m-d H:i:s',$booked[$k+1]);

    if( $t1 >= date_timestamp_get($st) && $t2 <= date_timestamp_get($en)  )
    $avoid = true;   //yes. booked
    }
$slots =[ $i->format('H:i'),$i->modify("+20 minutes")->format('H:i')];
if(!$avoid && $i<$end)  //if not booked and less than end time
{
$count++;  
array_push($out,$slots);  //add slot to array
}
}
var_dump($out);   //array out
echo $count ." of slots available";

Upvotes: 6

whyguy
whyguy

Reputation: 800

You have to loop through slops up to the last sloop (which is less or equal to end time) at the same time we incease start time with 20 minutes each time and add it to the data array.

You can see more on PHP documentation page about functions I used: strtotime, date

<?php

    $start_time = strtotime('2015-10-21 09:00:00');
    $end_time = strtotime('2015-10-21 19:45:00');
    $slot = strtotime(date('Y-m-d H:i:s',$start_time) . ' +20 minutes');

    $data = [];

    for ($i=0; $slot <= $end_time; $i++) { 

        $data[$i] = [ 
            'start' => date('Y-m-d H:i:s', $start_time),
            'end' => date('Y-m-d H:i:s', $slot),
        ];

        $start_time = $slot;
        $slot = strtotime(date('Y-m-d H:i:s',$start_time) . ' +20 minutes');
    }

    print_r($data);

?>

Upvotes: 2

Related Questions