我必须约会。现在,我需要找出这两者之间的差异,以便进一步计算。
我尝试了不同的方法,但我无法解决问题。谁能告诉我最好的方法呢。
我的代码是:
public function leaveRequest(request $request)
{
$fdate=$request->Fdate;
$tdate=$request->Tdate;
$start = Carbon::parse($fdate)->format('Y/m/d');
$end = Carbon::parse($tdate)->format('Y/m/d');
$days = $end->diffInDays($start);
/*$days=date_diff($end,$start);*/
echo $days;
exit;
$user = User::findOrFail(Auth::user()->id);
Mail::send('pages.leave', ['user' => $request,'userId'=>$user], function ($m) use ($request) {
$m->to($request->Email)->subject('Leave Request!');
});
DB::table('leaves')->insert(
['user' => Auth::user()->id, 'request_date' => Carbon::now(),'start' => $start,'end' => $end,'permissions' => "Pending",'leave_status' => "Active"]
);
return redirect()->back()->with('message','Your request has sent');
}
如果我可以得到这些日子,那么我可以将它插入树叶表中。
答案 0 :(得分:6)
你不需要Carbon,你可以使用简单的PHP来做到这一点。最好是使用PHP OOP方式。像这样:
$fdate = $request->Fdate;
$tdate = $request->Tdate;
$datetime1 = new DateTime($fdate);
$datetime2 = new DateTime($tdate);
$interval = $datetime1->diff($datetime2);
$days = $interval->format('%a');//now do whatever you like with $days
PS:DateTime不是一个函数,它是一个类,所以不要忘记在控制器顶部添加:use DateTime;
,以便它可以引用右根类或使用\ DateTime()代替在制作它的实例时。
我希望它有所帮助
答案 1 :(得分:3)
$to = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', '2015-5-5 3:30:34');
$from = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', '2015-5-6 9:30:34');
$diff_in_days = $to->diffInDays($from);
print_r($diff_in_days); // Output: 1
答案 2 :(得分:2)
任何使用PHP版本$fdate = $request->Fdate;
$tdate = $request->Tdate;
$datetime1 = strtotime($fdate); // convert to timestamps
$datetime2 = strtotime($tdate); // convert to timestamps
$days = (int)(($datetime2 - $datetime1)/86400); // will give the difference in days , 86400 is the timestamp difference of a day
答案 3 :(得分:1)
$fdate=$request->Fdate;
$tdate=$request->Tdate;
$to = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', $fdate);
$from = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', $tdate);
$diff_in_days = $to->diffInDays($from);
dd($diff_in_days);
全日文档:
答案 4 :(得分:0)
$from=Carbon::now()->subDays(5)->toDateString();
$ current = Carbon :: now()-> toDateString();
$ peoples = People :: whereBetween('joiningdate',array($ diff,$ current))-> get();