2

I have a bit of code that will change a string DateTime into DateTime objects

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; 

When I insert a string like '1999-01-01T00:00' I will get a object like

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"
}

So I was writing some test code on phpUnit and noticed an error here is the code I used and the results I got:

$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'

Why did datetime->format add a second to the time? When dumping out Class I see i doesn't contain the additional second. I looked around the internet everywhere and can't find the problem. If I change my code to

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

I get a passing test. Is this a bug or an unintended feature? I'm using PHP 7.3.9

Lance Coon
  • 23
  • 3

1 Answers1

0

In your formatting of the date in your test, you have...

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

as m is the month, this is why you get 1 as the last part, you should be using i for minutes...

$datetime->format('Y-m-d\TH:i')
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55