assertInstanceOf(\DateTimeImmutable::class, $now); $this->assertEquals('UTC', $now->getTimezone()->getName()); $this->assertLessThanOrEqual(2, abs(time() - $now->getTimestamp())); } public function testFromTimestamp(): void { $timestamp = 1609459200; // 2021-01-01 00:00:00 UTC $date = DateTime::fromTimestamp($timestamp); $this->assertEquals($timestamp, $date->getTimestamp()); $this->assertEquals('UTC', $date->getTimezone()->getName()); } public function testFromString(): void { $date = DateTime::fromString('2021-01-01 12:00:00'); $this->assertEquals('2021-01-01 12:00:00', $date->format('Y-m-d H:i:s')); $this->assertEquals('UTC', $date->getTimezone()->getName()); } public function testFromFormat(): void { $date = DateTime::fromFormat('01/01/2021', 'd/m/Y'); $this->assertEquals('2021-01-01', $date->format('Y-m-d')); } public function testFromDateTime(): void { $originalDate = new \DateTime('2021-01-01 12:00:00'); $immutableDate = DateTime::fromDateTime($originalDate); $this->assertInstanceOf(\DateTimeImmutable::class, $immutableDate); $this->assertEquals($originalDate->format('Y-m-d H:i:s'), $immutableDate->format('Y-m-d H:i:s')); } public function testCreateInterval(): void { $interval = DateTime::createInterval('P1D'); $this->assertInstanceOf(\DateInterval::class, $interval); $this->assertEquals(1, $interval->d); } public function testCreateTimezone(): void { $timezone = DateTime::createTimezone('Europe/Berlin'); $this->assertInstanceOf(\DateTimeZone::class, $timezone); $this->assertEquals('Europe/Berlin', $timezone->getName()); } public function testInvalidTimezoneThrowsException(): void { $this->expectException(InvalidTimezoneException::class); DateTime::createTimezone('Invalid/Timezone'); } public function testInvalidDateTimeThrowsException(): void { $this->expectException(InvalidDateTimeException::class); DateTime::fromString('not a date'); } public function testInvalidFormatThrowsException(): void { $this->expectException(InvalidDateTimeException::class); DateTime::fromFormat('2021-01-01', 'invalid format'); } public function testInvalidIntervalThrowsException(): void { $this->expectException(InvalidDateTimeException::class); DateTime::createInterval('invalid interval'); } public function testSetDefaultTimezone(): void { DateTime::setDefaultTimezone('Europe/Berlin'); $now = DateTime::now(); $this->assertEquals('Europe/Berlin', $now->getTimezone()->getName()); } public function testToday(): void { $today = DateTime::today(); $expected = (new \DateTimeImmutable('today'))->format('Y-m-d'); $this->assertEquals($expected, $today->format('Y-m-d')); $this->assertEquals('00:00:00', $today->format('H:i:s')); } public function testTomorrow(): void { $tomorrow = DateTime::tomorrow(); $expected = (new \DateTimeImmutable('tomorrow'))->format('Y-m-d'); $this->assertEquals($expected, $tomorrow->format('Y-m-d')); $this->assertEquals('00:00:00', $tomorrow->format('H:i:s')); } public function testYesterday(): void { $yesterday = DateTime::yesterday(); $expected = (new \DateTimeImmutable('yesterday'))->format('Y-m-d'); $this->assertEquals($expected, $yesterday->format('Y-m-d')); $this->assertEquals('00:00:00', $yesterday->format('H:i:s')); } }