2016-12-18 127 views
1

我想計算時差並將其插入到數據庫中...我的模型名稱是「預訂」,開始時間和結束時間將從用戶輸入..and total_duration將從這兩個計算並將插入到數據庫中...我使用這些代碼...但不會working.this是我的控制器。laravel時間計算從用戶輸入

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

    } 

}

回答

1

採用碳進行計算的執行時間是不是很不錯的主意。只需使用普通的老microtime()

$start = microtime(true); 
.... // Do something here. 
$end = microtime(true); 
$time = $end - $start; 
+1

thnx,它完全工作:D – incorporeal

1

我想你應該首先將其轉換成秒

$totalDuration = $finishTime->diffInSeconds($startTime); 

,然後希望格式

gmdate('H:i:s', $totalDuration); 

或者試試這個,如果這對你的工作

$finishTime->diff($startTime)->format('%H:%i:%s'); 
+0

它顯示碳沒有找到..我嘗試了命名空間碳;並使用Carbon/Carbon – incorporeal

+0

試試這個 - use \ Carbon \ Carbon 在開始時使用\是指一種全局命名空間。 –