Crane1912
Crane1912

Reputation: 23

How to get End Date, if start date and number of days are given in laravel 5.3

I'am writing a function in laravel that calculates the end date given the start date and no. of days and stores it in the database.

This is what I have come up with:

  public function store(Request $request)
   {
        $it = new iternary;

        $it->StartDate = Input::get("StartDate");

        $it->NoPeople = Input::get("NoPeople");

        $it->NoDays = Input::get("NoDays");

        (int)$ttemp=$it->NoDays;

        $it->budget = Input::get("budget");

        $EndDate = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + $it-
         >NoDays, date('Y')));

        $it->EndDate = $EndDate;

        $it->save();

        return view('success');

     }

But this gives me the wrong output. It gives me end date same as start date. Can you'll help please. Thanks

Upvotes: 0

Views: 2227

Answers (2)

Sandeep
Sandeep

Reputation: 21164

You can do it like this,

$startDate = new \Carbon\Carbon('2017-05-09')
$endDate = $startDate->addDays('10')

Further info about adding and subtracting in carbon can be found here here

You can also use PHP's default DateTime class for modification,

$date = new DateTime('2017-05-09')
$date->modify('+ 10 days')
$date->modify('-1 month')
$date->modify('next year')
$date->modify('previous month')

and so on

Upvotes: 2

Bhaskar Jain
Bhaskar Jain

Reputation: 1691

I am not much aware about Carbon dates in laravel. But in PHP, you can try this:

$startDate = '2017-05-09';
$days = 3;
$dayStr = $days == 1 ? 'day' : 'days';

echo date('Y-m-d', strtotime('+ '.$days. ' '.$dayStr, strtotime($startDate )));

Demo

Upvotes: 0

Related Questions