Home > database >  CarbonInterval , undefined variable $slots
CarbonInterval , undefined variable $slots

Time:01-31

<div >
                    <label for="starttime"> Start time </label>
                    <input type="time" name="starttime" starttime"
                           value = 12:00
                           }}"
                           
                           
                           >
                    
                    <span  role="alert">
                        <strong></strong>
                    </span>
                  
                </div>
                
<div >
                    <label for="starttime"> end time </label>
                    <input type="time" name="endtime" endtime"
                           value = 05:00
                           }}"
                           
                           
                           >
                    
                    <span  role="alert">
                        <strong></strong>
                    </span>
                  
                </div>
 public function storetimeintervals(SlotStoreRequest $request){

        $starttime = $request->starttime;
        $interval = $request->interval;
        $endtime = $request->endtime;
        
        $intervals = CarbonPeriod::since($starttime)->minutes($interval)->until($endtime)->toArray();
        foreach($intervals as $slot){

            $slots= Slot::create([
            
                'nslot'=> $slot->format('H:i'),
                
            
            ]);

        }
        if(!$slots){
            return redirect()->back()->with('error','Sorry, there a problem happened while creating slot'); 
        }
        return redirect()->route('slots.index')->with('success','The slots have been created');

        
    }

I am using carbon interval to get timeslot between two time . It works fine but when I input start time in PM then the end time in AM it cause $slots undefined. Other than that it work without error

I am expecting to input between two time without error. Hope you all can help me

CodePudding user response:

With the below payload

{
    "starttime" : "12:00PM",
    "endtime" : "05:00AM",
    "interval": 5    
}

I found this working . Better you convert your requested time in the proper format first.

if(strtotime($request->endtime) < strtotime($request->starttime)){
        $starttime = Carbon::parse($request->starttime)->format("Y-m-d H:i:s");
        $endtime = Carbon::parse($request->endtime)->addDay(1)->format("Y-m-d H:i:s");
} else {
        $starttime = Carbon::parse($request->starttime)->format("H:i:s");
        $endtime = Carbon::parse($request->endtime)->format("H:i:s");
}
$interval = $request->interval;

$intervals = CarbonPeriod::since($starttime)->minutes($interval)->until($endtime)->toArray();
    foreach($intervals as $slot){

        $slots= Slot::create([            
            'nslot'=> $slot->format('H:i')
        ]);

    }

OR Check this fiddle

https://laravelplayground.com/#/snippets/23bd3a21-de15-45b0-9085-65a93c841d58

  • Related