cakePHP使用I18 Time addMinutes函数更新每个相关变量,而不仅仅是当前示例

gmxoilav  于 2022-11-11  发布在  PHP
关注(0)|答案(1)|浏览(112)

I've been trying to set start times for different flights of competitors. The addMinutes function seems to update every variable associated with the "ONE" I'm trying to alter. Is this the expected result, or am I missing something really simple?
Thanks for your assistance,

public function test_i18_addminutes( $a_time = null )
{
    $b_time = $a_time ?: Time::now();

    for ( $i = 1; $i < 5; $i++ ) {
        $use_time = $b_time;
        debug( "BTIME: $b_time   USETIME: $use_time" );

        for ( $j = 1; $j < 7; $j++ ) {
            $mytime = $use_time->addMinutes( 15.0 );
            debug( "     MYTIME: $mytime      BTIME: $b_time   USETIME: $use_time" );
        }
    }
    $this->redirect( '/' );
}

\src\Controller\TestsController.php (line 89)
'BTIME: 9/14/21, 6:09 PM USETIME: 9/14/21, 6:09 PM'
' MYTIME: 9/14/21, 6:24 PM BTIME: 9/14/21, 6:24 PM USETIME: 9/14/21, 6:24 PM'
' MYTIME: 9/14/21, 6:39 PM BTIME: 9/14/21, 6:39 PM USETIME: 9/14/21, 6:39 PM'
' MYTIME: 9/14/21, 6:54 PM BTIME: 9/14/21, 6:54 PM USETIME: 9/14/21, 6:54 PM'
' MYTIME: 9/14/21, 7:09 PM BTIME: 9/14/21, 7:09 PM USETIME: 9/14/21, 7:09 PM'
' MYTIME: 9/14/21, 7:24 PM BTIME: 9/14/21, 7:24 PM USETIME: 9/14/21, 7:24 PM'
' MYTIME: 9/14/21, 7:39 PM BTIME: 9/14/21, 7:39 PM USETIME: 9/14/21, 7:39 PM'
'BTIME: 9/14/21, 7:39 PM USETIME: 9/14/21, 7:39 PM'
' MYTIME: 9/14/21, 7:54 PM BTIME: 9/14/21, 7:54 PM USETIME: 9/14/21, 7:54 PM'
' MYTIME: 9/14/21, 8:09 PM BTIME: 9/14/21, 8:09 PM USETIME: 9/14/21, 8:09 PM'

wpx232ag

wpx232ag1#

所有变量a_timeb_timeuse_time都指向同一个对象,而且\Cake\I18n\Time是可变的,所以$mytime也是同一个对象。
如果你想要不可变的时间对象,请使用\Cake\I18n\FrozenTime。这些对象的时间操作方法将返回新的示例。
另请参阅

*Cookbook〉日期和时间〉不可变的日期和时间

相关问题