我正在使用laravel 5.5试图计算天数并乘以价格
当我从下拉列表中选择日期时工作正常,但是当我输入日期计算错误时发生的事情我不知道
这是我的控制器
$pick = Carbon::parse($request->input('pickdate'));
$drop = Carbon::parse($request->input('dropdate'));
$lengthday = $pick->diffInDays($drop, false);
$ptime = str_replace([' '], '', $request->pickuptime);
$dtime = str_replace([' '], '', $request->droptime);
$picktime = Carbon::parse($ptime);
$droptime = Carbon::parse($dtime);
$lengthmin = $picktime->diffInMinutes($droptime, false);
$car = Car::where('slug', $car)->firstorfail();
$cprice = $car->price;
if ($lengthmin >= '1') {
$minprice = $car->price;
}
else
{
$minprice = '0';
}
$price = $cprice*$lengthday+$minprice;
if ($price <= '0') {
return back()->with('warning', 'Please Enter Valid Date & Time');
}
这是我的观点
<div class="form-date-w3-agileits">
<div class="form-agileits">
<label>Pickup Date :</label>
</div>
<div class="form-agileits-2">
<input type="date" name="pickupdate" style="width: 92%;
outline: none;
font-size: 0.9em;
padding: 13px 10px;
border: 1px solid #fff;
font-weight: 100;
-webkit-appearance: none;
margin-bottom: 1.4em;
background: rgba(0, 0, 0, 0.69);
font-family: 'Amaranth', sans-serif;
color: #bbb9b9;" required="">
</div>
<div class="clear"> </div>
</div>
答案 0 :(得分:1)
在评论中,您说只有在用户手动输入日期时才会出现问题。您需要使用date
验证规则以允许用户仅输入有效日期。例如:
'pickdate' => 'date',
'dropdate' => 'date',
然后您确定Carbon将能够解析输入数据。