2

I'm projects table that users projects is saved in it and have start_time and finish_time and price,I give it as form that has this code:

<div class="form-group">
   <input type="date" class="form-control" name="start_time" >
 </div>

 <div class="form-group">
 <input type="date" class="form-control" name="finish_time" >
 </div>

 <div class="form-group">
 <input type="text" class="form-control" name="price" >
 </div>

Now,I Want subtract dates and if result be Negative Can multiply price and result,How I can do this?

3
  • Use Carbon Class Commented Oct 7, 2017 at 17:29
  • please write as code for me Commented Oct 7, 2017 at 17:30
  • 4
    Stack Overflow is not a Free Coding Platform. Here is the documentation carbon.nesbot.com/docs ... try it yourself! If you have something that you believe should work but do not, post it here and we are glade to help you. Commented Oct 7, 2017 at 17:35

3 Answers 3

5

What you should do is something like this:

$start_time = \Carbon\Carbon::parse($request->input('start_time'));
$finish_time = \Carbon\Carbon::parse($request->input('finish_time'));

$price = $request->input('price');

$result = $start_time->diffInDays($finish_time, false);

if ($result < 0) {
  $price = $price * $result;
}

I don't know which dates you wanted to subtract so make sure that you valid one. You should use either:

$result = $start_time->diffInDays($finish_time, false);

or

$result = $finish_time->diffInDays($start_time, false);

Keep in mind you need to pass false as 2nd argument to diffInDays method - otherwise you will always get positive result.

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

Comments

0

In your controller method you can calculate the difference:

   $start_time = $request->input('start_time');
   $start_time_ob = new \Carbon\Carbon($start_time);

Or you can use the static Carbon::parse() method:

$finish_time = $request->input('finish_time');
$finish_time_ob = \Carbon\Carbon::parse($date);

For your purposes you can use the this full example:

$diff =  $finish_time_ob->diffInDays($start_time_ob);

Comments

0

Expiry Date

$expirydate = \Carbon\Carbon::parse($object->end_date);

$expirydate = 2020-06-09 00:00:00

Returns Current Date

$today = \Carbon\Carbon::now();

$today = 2019-09-05 13:53:47

Total number of day days remaining to expire

$difference = $today->diffInDays($expirydate, false)

$difference = 277

Total 277 days remaining to expire.

If Days are coming in negative number it means it has been expired otherwise yet to expire.

Comments

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.