Ad

How To Add Number Of Days To A Date?

- 1 answer

My code is like this :

<table class="table">
    <tbody>
        <tr>
            <th>Date</th>
            <th>Charge</th>
        </tr>
        @foreach($cancel_policy_api as $key=>$value)
        <tr>
            <td>{{ $value['CheckIn'] }} + {{ $value['CXLDay'] }}</td>   
            <td>{{ $value['CXLFee'] }} </td>

        </tr>   
        @endforeach
    </tbody>
</table>

$value['CheckIn'] = '2016-01-23'
$value['CXLDay'] = '2'

I want to add CheckIn and CXLDay

So, $value['CheckIn'] + $value['CXLDay'] = 2016-01-25

How to add integer/string and date in laravel 5 view?

Thank you very much

Ad

Answer

You can do that with the strtotime() function, then use date() to get it back to a date again - like this:

$newTime = strtotime('%s +%d days'. $value['CheckIn'], $value['CXLDay']);
$newDate = date('Y-m-d', $newTime);

Or, since you're using Laravel, if this is a Carbon object you can just use this:

$newDate = $oldDate->addDays($numDays);

(Docs for the latter here: http://carbon.nesbot.com/docs/#api-addsub)

Either way, I would recommend that you don't do this in the view, but either through a model method or in the controller, or in a repository - depending on what best suits your application.

Ad
source: stackoverflow.com
Ad