I am working on a free date time slots. I am stuck on creating proper spilts array and displaying available time slots .

I want to spilt into 2 Hour slot possible from start and end time.

    foreach ( $period as $dt ){
                $m = [
                    'start'=>date('Y-m-d ',strtotime($dt->format('Y-m-d').' -1 
                     day')).$max,
                    'end'=>$dt->format('Y-m-d ').$min
                ];
                foreach($free as $k=>$f){
                    if($m['start']>$f['start'] && $m['start']<=$f['end']){
                        $free[$k]['end'] = $m['start'];
                        if($m['end']<$f['end'])

                            $free[] = ['start'=>$m['end'], 'end'=>$f['end']];
                    }
                    elseif($m['end']<$f['end'] && $m['end']>$f['start']){
                        $free[$k]['start'] = $m['end'];
                    }
                }
            }

So for example, the meeting times array above should produce the following array of free time:

Array
(
    [0] => Array
        (
            [start] => 2019-05-08 07:00:00
            [end] => 2019-05-08 17:00:00
        )

    [1] => Array
        (
            [start] => 2019-05-09 11:25:00
            [end] => 2019-05-09 12:40:00
        )

    [2] => Array
        (
            [start] => 2019-05-09 14:15:00
            [end] => 2019-05-09 14:30:00
        )

    [3] => Array
        (
            [start] => 2019-05-10 07:00:00
            [end] => 2019-05-10 17:00:00
        )

    [4] => Array
        (
            [start] => 2019-05-09 07:00:00
            [end] => 2019-05-09 07:40:00
        )

    [5] => Array
        (
            [start] => 2019-05-11 07:00:00
            [end] => 2019-05-11 17:00:00
        )

)

Expected Output :

  Array
  (
    [0] => Array
        (
            [0] => Array( 
                   [start] => 2019-05-08 07:00:00
                   [end] => 2019-05-08 09:00:00
             )

            [1] => Array( 
                   [start] => 2019-05-08 09:00:00
                   [end] => 2019-05-08 11:00:00
             )
            [2] => Array( 
                   [start] => 2019-05-08 11:00:00
                   [end] => 2019-05-08 13:00:00
             )
            [3] => Array( 
                   [start] => 2019-05-08 13:00:00
                   [end] => 2019-05-08 15:00:00
             )
            [4] => Array( 
                   [start] => 2019-05-08 15:00:00
                   [end] => 2019-05-08 17:00:00
             )

        )

    [1] => Array
        (
            [start] => 2019-05-09 11:25:00
            [end] => 2019-05-09 12:40:00
        )

.. so on ...

2 Answers

0
Rakesh Kumar Jakhar On

You can approach this as

foreach($arr as $k => $v){
$diff = strtotime($v['end'])-strtotime($v['start']);
if($diff >= 7200){
    for($i=strtotime($v['start']);$i<strtotime($v['end']);){
        $start = date('Y-m-d H:i:s',$i);
        $i     = $i+7200;
        $end   =  date('Y-m-d H:i:s',$i);
        $res[$k][] = [
            'start' => $start,
            'end'   => $end
        ];
    }
 }else{
    $res[$k][] = [
            'start' => date('Y-m-d H:i:s',strtotime($v['start'])),
            'end'   => date('Y-m-d H:i:s',strtotime($v['end']))
        ];
 }
}

Live Demo

0
Eakethet On

I would suggest using \DateTime, its quite comfortable to use

<?php

$start = new \DateTime('2019-05-08 07:00:00');
$end = new \DateTime('2019-05-08 07:30:00');

function getSlots(\DateTime $start, \DateTime $end): array
{
    $slots = [];
    while ($start < $end) {
        $old = clone($start);
        $start->modify('+2 hours');

        if ($start > $end) {
            echo 1;
            $slots[] = [
                'from' => $old,
                'to' => $end
            ];
            return $slots;
        } else {
            $slots[] = [
                'from' => $old,
                'to' => clone($start)
            ];
        }
    }
    return $slots;
}

var_dump(getSlots($start, $end));

And you can try it out there https://3v4l.org/qAatj