在拉拉韦尔寻找两个日期之间的天 [英] Finding days between two dates in laravel

查看:65
本文介绍了在拉拉韦尔寻找两个日期之间的天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须约会.现在,我需要找到这两者之间的差异以进行进一步的计算.

I have to dates. Now, I need to find the difference between these two for further calculations.

我尝试了不同的方法,但无法解决问题.谁能告诉我最好的方法.

I tried different ways but I am not able to fix the issues. Can anyone tell me the best way to do it.

我的代码是:

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');
}

如果我可以得到日子,那么可以将其插入叶子表中.

If I can get the days then I can insert it into the leaves table.

推荐答案

您不需要Carbon,可以使用简单的PHP来实现.最好是使用PHP OOP方式.像这样:

You don't need Carbon, you can do that using simple PHP. Best is to use PHP OOP way. Like this:

$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不是一个函数,而是一个类,因此请不要忘记在控制器顶部添加:使用DateTime; ,以便它可以引用正确的根类或使用 \ DateTime(),而不是创建它的实例.

PS : DateTime is not a function, its a class so don't forget to add : use DateTime; at top of your controller so that it can reference to right root class or use \DateTime() instead when making its instance.

我希望对您有帮助

这篇关于在拉拉韦尔寻找两个日期之间的天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆