laravel 如何将天数添加到日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34962735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:05:30  来源:igfitidea点击:

How to add number of days to a date?

laravellaravel-5

提问by moses toh

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

我想添加 CheckIn 和 CXLDay

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

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

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

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

Thank you very much

非常感谢

回答by Joel Hinz

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

您可以使用该strtotime()函数执行此操作,然后使用date()它再次将其恢复为日期 - 如下所示:

$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:

或者,由于您使用的是 Laravel,如果这是一个 Carbon 对象,您可以使用它:

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

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

(后者的文档在这里: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.

无论哪种方式,我都建议您不要在视图中执行此操作,而是通过模型方法或在控制器中或在存储库中执行此操作——这取决于什么最适合您的应用程序。

回答by ruggfrancesco

Edit: you are trying to sum days contained in a variable, so try this

编辑:您正在尝试对变量中包含的天数求和,所以试试这个

$mydate = '23-01-2016';
$daystosum = '2';
$datesum = date('d-m-Y', strtotime($mydate.' + '.$daystosum.' days'));