laravel PHP Carbon 类更改了我的原始变量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34413877/
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 12:51:27  来源:igfitidea点击:

PHP Carbon class changing my original variable value

phplaravelphp-carbon

提问by Johan Bj?rklund

I'm trying to make a few navigation buttons in a calendar type thing I'm creating, and I'm using carbon to create the dates.

我正在尝试在我正在创建的日历类型的东西中制作一些导航按钮,并且我正在使用 carbon 来创建日期。

This is the code in the controller:

这是控制器中的代码:

if ($date == null) {
    $date = \Carbon\Carbon::now();
} else {
    $date = \Carbon\Carbon::createFromFormat('Y-m-d', $date);
}
$navDays = [
    '-7Days' => $date->subDay('7')->toDateString(),
    '-1Day'  => $date->subDay('1')->toDateString(),
    'Today'    => $date->today()->toDateString(),
    '+1Day'  => $date->addDay('1')->toDateString(),
    '+7Days' => $date->addDay('7')->toDateString()
];

and then I'm my view, I'm doing this:

然后我是我的观点,我正在这样做:

@foreach($navDays as $key => $i)
    <li>
        <a href="/planner/bookings/{{ $i }}" class="small button">
            {{ $key }}
        </a>
    </li>
@endforeach

This problem is, that carbon seems to change the $date during the array creating, because these are the dates I'm getting(with $datebeing set to 2015-11-29):

这个问题是,carbon 似乎在数组创建期间更改了 $date,因为这些是我正在获取的日期($date设置为2015-11-29):

<ul class="button-group even-5">
    <li><a href="/planner/bookings/2015-11-22" class="small button">-7Days</a></li>
    <li><a href="/planner/bookings/2015-11-21" class="small button">-1Day</a></li>
    <li><a href="/planner/bookings/2015-12-22" class="small button">Today</a></li>
    <li><a href="/planner/bookings/2015-11-22" class="small button">+1Day</a></li>
    <li><a href="/planner/bookings/2015-11-29" class="small button">+7Days</a></li>
</ul>

Does anybody know what I'm doing wrong?

有谁知道我做错了什么?

回答by diggersworld

When you run these methods against a Carbon object it updates the object itself. Therefore addDay()moves the value of Carbon one day forward.

当您针对 Carbon 对象运行这些方法时,它会更新对象本身。因此,addDay()将 Carbon 的价值向前移动一天。

Here's what you need to do:

您需要执行以下操作:

$now = Carbon::now();

$now->copy()->addDay();
$now->copy()->addMonth();
$now->copy()->addYear();
// etc...

The copy method essentially creates a new Carbon object which you can then apply the changes to without affecting the original $nowvariable.

copy 方法本质上创建了一个新的 Carbon 对象,然后您可以将更改应用于该对象,而不会影响原始$now变量。

To sum up, the methods for copying a Carbon instance are:

总结一下,复制一个Carbon实例的方法有:

  • copy
  • clone- an alias of copy
  • copy
  • clone- 别名 copy

Check out the documentation: https://carbon.nesbot.com/docs/

查看文档:https: //carbon.nesbot.com/docs/

回答by Mark Baker

The problem is that you're assuming that subDay()/addDay() don't change the date object, whereas they do.... they're just wrapping around the DateTimeobject modify()method:

问题是您假设 subDay()/addDay() 不会更改日期对象,而它们会更改....它们只是围绕DateTime对象modify()方法进行包装:

DateTime::modify -- date_modify — Altersthe timestamp

DateTime::modify -- date_modify --改变时间戳

(my emphasis)

(我的重点)

Instead, use

相反,使用

$navDays = [
    '-7Days' => (clone $date)->subDay('7')->toDateString(),
    '-1Day'  => (clone $date)->subDay('1')->toDateString(),
    'Today'  => (clone $date)->today()->toDateString(),
    '+1Day'  => (clone $date)->addDay('1')->toDateString(),
    '+7Days' => (clone $date)->addDay('7')->toDateString()
];

回答by Yevgeniy Afanasyev

Docosays

多科

You can also create a copy() of an existing Carbon instance. As expected the date, time and timezone values are all copied to the new instance.

您还可以创建现有 Carbon 实例的 copy() 。正如预期的那样,日期、时间和时区值都被复制到新实例中。

$dt = Carbon::now();
echo $dt->diffInYears($dt->copy()->addYear());  // 1

// $dt was unchanged and still holds the value of Carbon:now()