Skip to content

Commit

Permalink
Add LocalDate::daysUntil() method
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Nov 14, 2018
1 parent cba7214 commit d1070ad
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/LocalDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,21 @@ public function until(LocalDate $endDateExclusive) : Period
return Period::of($years, $months, $days);
}

/**
* Calculates the number of days between this date and another date.
*
* The start date is included, but the end date is not.
* For example, `2018-02-15` to `2018-04-01` is 45 days.
*
* @param LocalDate $endDateExclusive
*
* @return int
*/
public function daysUntil(LocalDate $endDateExclusive) : int
{
return $endDateExclusive->toEpochDay() - $this->toEpochDay();
}

/**
* Returns a local date-time formed from this date at the specified time.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/LocalDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,35 @@ public function providerUntil() : array
];
}

/**
* @dataProvider providerDaysUntil
*
* @param string $date1
* @param string $date2
* @param int $expectedDays
*/
public function testDaysUntil(string $date1, string $date2, int $expectedDays)
{
$date1 = LocalDate::parse($date1);
$date2 = LocalDate::parse($date2);

$this->assertSame($expectedDays, $date1->daysUntil($date2));
}

/**
* @return array
*/
public function providerDaysUntil() : array
{
return [
['2018-01-01', '2020-01-01', 730],
['2020-01-01', '2022-01-01', 731],
['2018-01-15', '2018-02-15', 31],
['2018-02-15', '2018-03-15', 28],
['1900-02-18', '2031-09-27', 48068]
];
}

public function testAtTime()
{
$localDateTime = LocalDate::of(1, 2, 3)->atTime(LocalTime::of(4, 5, 6, 7));
Expand Down

0 comments on commit d1070ad

Please sign in to comment.