PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 11, 2022

[FIXED] cakePHP use of I18 Time addMinutes function updates every related variable, not just the current instance

 March 11, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

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'


Solution

All your variables, a_time, b_time, and use_time, point to the same object. Furthermore \Cake\I18n\Time is mutable, so $mytime will also be the very same object.

If you want immutable time objects, use \Cake\I18n\FrozenTime instead. The time manipulation methods of those objects will return new instances.

See also

  • Cookbook > Date & Time > Immutable Dates and Times


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing