简体   繁体   English

PHP:使用DateTime-> modify两次,结果相同

[英]PHP: Using DateTime->modify Two Times, Same Result

I want to have two variables. 我想要两个变量。 one -2 seconds of a time and another +2 seconds of the time. 一次-2 seconds ,另一次+2 seconds This is my code: 这是我的代码:

$myTime = '2015-08-17 08:19:26';
$myTime = DateTime::createFromFormat('Y-m-d H:i:s', $myTime);

echo '<pre>';

var_dump( $myTime );

$myTime->modify('+2 seconds');
$myTime_p2 = $myTime;
$myTime->modify('-4 seconds');
$myTime_m2 = $myTime;

var_dump( $myTime );
var_dump( $myTime_p2 );
var_dump( $myTime_m2 );

echo '</pre>';

The result is not as expected: 结果与预期不同:

object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-08-17 08:19:26.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "America/New_York"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-08-17 08:19:24.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "America/New_York"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-08-17 08:19:24.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "America/New_York"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-08-17 08:19:24.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(16) "America/New_York"
}

As you see the $myTime_p2 and $myTime_m2 variables have the same value. 如您所见, $myTime_p2$myTime_m2变量具有相同的值。 So where is the problem? 那么问题出在哪里呢?

This should work: 这应该工作:

$myTime->modify('+2 seconds');
$myTime_p2 = clone $myTime;
$myTime->modify('-4 seconds');
$myTime_m2 = clone $myTime;

UPDATE 更新
Using DateTimeImmutable as per @Im0rtality's comment below: 根据以下@ Im0rtality的注释使用DateTimeImmutable

$myTime = '2015-08-17 08:19:26';
$date = DateTimeImmutable::createFromFormat('Y-m-d H:i:s',$myTime);

print $date->modify('+2 seconds')->format('Y-m-d H:i:s') . PHP_EOL;
print $date->modify('-2 seconds')->format('Y-m-d H:i:s') . PHP_EOL;

/*
2015-08-17 08:19:28
2015-08-17 08:19:24
*/

You need to clone the initial object to get the two variations 您需要克隆初始对象以获取两个变体

$myTime = '2015-08-17 08:19:26';
$myTime = DateTime::createFromFormat('Y-m-d H:i:s', $myTime);

echo '<pre>';

var_dump( $myTime );

$myTime_p2 = clone $myTime;
$myTime_p2->modify('+2 seconds');

$myTime_m2 = clone $myTime;
$myTime_m2->modify('-4 seconds');

var_dump( $myTime );
var_dump( $myTime_p2 );
var_dump( $myTime_m2 );

echo '</pre>';

<pre>object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-08-17 08:19:26.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/Berlin"
}
object(DateTime)#1 (3) {
  ["date"]=>
  string(26) "2015-08-17 08:19:26.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/Berlin"
}
object(DateTime)#2 (3) {
  ["date"]=>
  string(26) "2015-08-17 08:19:28.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/Berlin"
}
object(DateTime)#3 (3) {
  ["date"]=>
  string(26) "2015-08-17 08:19:22.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/Berlin"
}
</pre>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM