如何在日期中增加天数? [英] How to add number of days to a date?

查看:123
本文介绍了如何在日期中增加天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是这样的:

<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'

我要添加CheckIn和CXLDay

I want to add CheckIn and CXLDay

所以$value['CheckIn'] + $value['CXLDay'] = 2016-01-25

如何在laravel 5视图中添加整数/字符串和日期?

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

非常感谢您

推荐答案

您可以使用strtotime()函数执行此操作,然后使用date()将其重新恢复为日期-像这样:

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);

或者,因为您使用的是Laravel,如果这是Carbon对象,则可以使用以下代码:

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

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

(后者的文档在这里: http://carbon.nesbot.com/docs/#api-addsub )

(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.

这篇关于如何在日期中增加天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆