2

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. 
ITit superpower
  • 506
  • 2
  • 7
  • 15

2 Answers2

6

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";
Subin Thomas
  • 1,408
  • 10
  • 19
  • hello thanks for this. but rather then string. i have start and end time different field for booked. same for new book. so can you change on it. thanks again :) – ITit superpower Oct 21 '15 at 11:03
  • my db have datetime formate so i used booked date time as => for ex. booked time b_start = 2015-10-21 12:00:00 b_end = 2015-10-21 12:30:00 and same for slot i want start slot and end slot in different variable so i can print only start time in my output. and submit start and end both different in database. thanks a lot :) – ITit superpower Oct 21 '15 at 11:10
  • inshort in your given slot - slot is right but i want start time and end time as variable in array so i can save that in my database. same function for booked start time is. 2015-10-21 01:00:00 so its one field and 2nd field is 2015-10-21 01:30:00 Thanks so inshort i want slot with start and end variable and same from select query i got start and end booked time in this above datetime formate. Thanks – ITit superpower Oct 21 '15 at 11:17
  • INSHORT I HAVE 2 TYPE SLOT LIKE EVEN I HAVE 2ND SERVICE WHICH HAVE 40 MIN SLOT SO SYSTME WILL MAKE 40 MIN SLOT BUT I WILL AVODE THE 20 MIN BOOKING SLOT. SO PLEASE consider this also thanks its final. query – ITit superpower Oct 21 '15 at 11:20
  • ok thanks slot are fine now. but i want if i set that 12-30 no slot then i dont want slot which include this time. right now it not filter booked time. Bcz getting this warning Warning: date_timestamp_get() expects parameter 1 to be DateTime, boolean given in line 22 – ITit superpower Oct 21 '15 at 13:22
  • if( $t1 == date_timestamp_get($st) && $t2 ==date_timestamp_get($en) ) its warning. .slot array got in output. but it also include 12 to12:20 and etc. – ITit superpower Oct 21 '15 at 13:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92969/discussion-between-subin-thomas-and-itit-superpower). – Subin Thomas Oct 21 '15 at 13:28
  • thanks see in chat. what if system create 3 hour slot like 9 to 12 12 to 3 and 3 to 6 but still he is ignoring 10:00 to 10-20 booking he is only count 9:00 to 9:20 but not counting 10. any suggestion.!! thanks – ITit superpower Oct 23 '15 at 06:59
  • need 1 more help in condition. now syttem will generate 3 slot 10 to 2 2 to 5 and 5 to 8 but he is ignoring 11 to 11-20 time. so i dont want to book on this time.!! thanks – ITit superpower Oct 23 '15 at 08:53
  • This is weird, when you make $avoid = true, its only in the inner loop => this means $avoid value will be set per the last item in the array?? – Ali Obeid Nov 14 '21 at 20:08
2

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);

?>
whyguy
  • 784
  • 7
  • 11
  • yes i will look but out of this i also want to not included booked time. like if ex 12:00-12:20 , 2:40 to 2:60 booked then it will not included in this list. – ITit superpower Oct 21 '15 at 10:13
  • if you don't know to make a array difference then go on google and see some php fundamentals tutorials... – whyguy Oct 21 '15 at 10:16
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/q/114762) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rizier123 Oct 21 '15 at 10:18
  • @whyguy thanks but. can you take 2 time slot as example if they are booked and not got in my array. thanks – ITit superpower Oct 21 '15 at 10:27