试图用碳 laravel 获取日期差异

Trying to get difference of dates with carbon laravel

我一直试图用 carbon 计算两个日期的差异,这样我就可以计算花费的百分比,但它一直输出 0

The below is meant to get the difference from the start date and end date.

  public function validityMeter($start_date, $end_date){

    $start_date = Carbon::parse($start_date);
    $diff =$start_date->diffInDays($end_date);

    return $diff;


}

public function percentageMeter($start_date, $end_date){


    $diff = Carbon::parse($start_date);
    $diff = $diff->diffInDays(Carbon::now()->format("Y-m-d"));       
    $multiple = $diff * 100;
    $percentage = $multiple / $this->validityMeter($start_date, $end_date);

    return $percentage;
}

The the second method (percentageMeter()) is meant to get the difference from the current date and start_date after which i will use the value of both methods to get the percentage.

Blade模板

 <div class="progress progress-xs">
                    <div class="progress-bar progress-bar-green" style="width: {{$obj->percentageMeter($booking->start_date, $booking->end_date)}}"> </div>
                  </div>

控制器

public function type($type){

    // dd(Carbon::now()->format("Y-m-d"));
    $booking = Booking::where("approve", true)
                        ->where("end_date", '>=', Carbon::now()->format("Y-m-d"))
                        ->where("plan_type", $type)
                        ->get();
                        // dd($booking);
    $obj = new Plan;

    $counter= 1;
    return view("admin.plan.type")->with("booking", $booking)
                                    ->with("counter", $counter)
                                    ->with("obj", $obj);
}

$start_date  = 2018-05-06 00:00:00;
$end_date = 2018-12-30

您在第二个函数中调用了 Carbon::now(),因此 $start_dateCarbon::now() 都是相同的日期。只需输入不同的开始日期,您就可以开始了。