Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/ZonedDateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,15 @@ public function plusDuration(Duration $duration): ZonedDateTime
return ZonedDateTime::ofInstant($this->instant->plus($duration), $this->timeZone);
}

/**
* Returns a Duration representing the time elapsed between this ZonedDateTime and the given one.
* This method will return a negative duration if the given ZonedDateTime is before the current one.
*/
public function getDurationTo(ZonedDateTime $that): Duration
{
return Duration::between($this->getInstant(), $that->getInstant());
}

/**
* Returns a copy of this ZonedDateTime with the specified period in years added.
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/ZonedDateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,29 @@ public function testToString(): void
$this->assertSame('2000-01-20T12:34:56.123456789-08:00[America/Los_Angeles]', (string) $zonedDateTime);
}

/**
* @dataProvider providerGetDurationTo
*/
public function testGetDurationTo(string $firstDate, string $secondDate, int $expectedSeconds, int $expectedNanos): void
{
$actualResult = ZonedDateTime::parse($firstDate)->getDurationTo(ZonedDateTime::parse($secondDate));

$this->assertDurationIs($expectedSeconds, $expectedNanos, $actualResult);
}

public function providerGetDurationTo(): array
{
return [
['2023-01-01T10:00:00Z', '2023-01-01T10:00:00Z', 0, 0],
['2023-01-01T10:00:00Z', '2023-01-01T10:00:10Z', 10, 0],
['2023-01-01T10:00:00.001Z', '2023-01-01T10:00:10.002Z', 10, 1000000],
['2023-01-01T10:00:00.001Z', '2023-01-01T13:00:10.002+03:00', 10, 1000000],
['2023-01-01T10:00:00.000000001Z', '2023-01-01T10:00:00.000000009Z', 0, 8],
['2023-01-01T10:00:00Z', '2023-01-02T10:00:00Z', 24 * 60 * 60, 0],
['2023-01-02T10:00:00Z', '2023-01-01T10:00:00Z', -24 * 60 * 60, 0],
];
}

private function getTestZonedDateTime(): ZonedDateTime
{
$timeZone = TimeZone::parse('America/Los_Angeles');
Expand Down