简体   繁体   中英

Laravel: How to add number of days to a date?

I have a form, where I insert the start_date field (date format) and I want to auto assign the end_date (+180 days) through controller, before store.

My store controller is very simple:

public function show($id)
{
    $ticket = Ticket::find($id);
    return view('tickets.show', compact('ticket'));
}

Equal my update controller:

public function update(Request $request, $id)
{
    $ticket = Ticket::find($id);
    $ticket->fill($request->all())->save();
    return redirect()->route('tickets.show', $ticket->id)->with('info', 'Datos actualizados con éxito');
}

Do you any idea?

Put this line after you initialize Ticket object:

$ticket->end_date = $request->input('start_date')->addDays($numDays);

edit-01:

in your model: define the following code.

protected $dates = ['created_at', 'updated_at', 'start_date '];

首先将您的字符串转换为DateTime ,然后执行

$date->add(new \DateInterval('P180D')); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM