2

i am using laravel 5.5 trying to calculate days and multiply with price

working fine when i select date from drop down but when i type date calculation doing wrong what's happening i don't know

here is my controller

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

here is my view

<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>
6
  • 1
    Typo? $request->input('pickdate') Your field is named pickupdate Commented Feb 5, 2018 at 13:08
  • 5
    Hard to help when you don't tell us what you input when it works, and what you input when it doesn't work.... Commented Feb 5, 2018 at 13:09
  • its work fine when someone select date from drop down its not working when someone type date from keyboard try for booking on xrentc.com Commented Feb 5, 2018 at 13:10
  • So is it because the user enters a date in a different format? Commented Feb 5, 2018 at 13:12
  • @RiggsFolly how can i fix this? Commented Feb 5, 2018 at 13:14

1 Answer 1

1

In the comments, you've said you have problems only when users enter the date manually. You need to use the date validation rule to allow users to enter only valid dates. For example:

'pickdate' => 'date',
'dropdate' => 'date',

Then you'll be sure Carbon will be able to parse the input data.

Sign up to request clarification or add additional context in comments.

1 Comment

i added current date as value and it fixed validation give me error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.