i want to calculate the time difference and insert it into database...my model name is "booking" and the start time and end time will be taken input from users..and total_duration will be calculated from these two and will be inserted into database...i use these codes...but won't working.this is my controller.
<?php
/*namespace App\booking;*/
use Carbon\Carbon;
namespace App\Http\Controllers;
use App\booking;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
class RoombookController extends Controller
{
public function showreport(Request $request)
{
/* dd($request->all());
*/ $time= Carbon.now();
$booking = new booking;
$booking->bookdate = $request->input('bookdate');
$booking->roomname = $request->input('roomname');
//echo $datefrom;
$booking->starttime =$startTime= $request->input('starttime');
$booking->endtime = $finishTime=$request->input('endtime');
$booking->purpose = $request->input('Purpose');
//echo $dateto;
$time->sTime = Carbon::parse($startTime);
$time->fTime = Carbon::parse($finishTime);
$time->total_time = $fTime->diffForHumans($sTime);
$booking->total_duration = $time->total_time;
$booking->save();
}
}
Using Carbon for calculation execution time is not very good idea. Just use plain old microtime()
:
$start = microtime(true);
.... // Do something here.
$end = microtime(true);
$time = $end - $start;
I think you should first convert it into seconds
$totalDuration = $finishTime->diffInSeconds($startTime);
and then desirable format
gmdate('H:i:s', $totalDuration);
or try this if this work for you
$finishTime->diff($startTime)->format('%H:%i:%s');