PHP 的日期时间格式方法正在 PHP 中添加一秒

PHP's DateTime Format method is adding a second in PHP

我有一些代码可以将字符串 DateTime 更改为 DateTime 对象

case 'datetime':
   if(!$value instanceof \DateTime){
   $variable = new \DateTime($value, new \DateTimeZone('US/Central'));
   if(!$variable){
      $errorarray = \Datetime::getLastErrors();
      throw new ErrorException('DateTime Error: '. explode(' ', $errorarray['errors']));
   }
break; 

当我插入像'1999-01-01T00:00'这样的字符串时,我会得到一个像

这样的对象
class DateTime#439 (3) {
  public $date =>
  string(26) "1999-01-01 00:00:00.000000"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(10) "US/Central"
}

所以我在 phpUnit 上写了一些测试代码,发现这里有一个错误是我使用的代码和我得到的结果:

$this->assertEquals('1999-01-01T00:00', $datetime->format('Y-m-d\TH:m'));
1) tests\genericObjectTest::testStrictType
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'1999-01-01T00:00'
+'1999-01-01T00:01'

为什么 datetime->format 会在时间上加一秒?转储 Class 时,我发现我不包含额外的秒数。我到处环顾互联网,找不到问题所在。如果我将代码更改为

$datetime = new \DateTime('1999-01-01T00:00', new \DateTimeZone('US/Central'));
$this->assertEquals($datetime, $this->GenericObject->getField('datetime'));

我通过了考试。这是错误还是意外功能?我正在使用 PHP 7.3.9

在你的测试日期格式中,你有...

$datetime->format('Y-m-d\TH:m')

因为 m 是月份,这就是为什么你得到 1 作为最后一部分的原因,你应该使用 i 分钟...

$datetime->format('Y-m-d\TH:i')