Skip to content

Commit

Permalink
Added support for future dates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jan 3, 2021
1 parent 4bc4625 commit 63564d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ final class Date
public static function adapt($date): string
{
$date = is_string($date) ? new \DateTimeImmutable($date) : $date;
$diff = $date->diff(new \DateTime);
$diff = (new \DateTime)->diff($date);
$days = $diff->days * ($diff->invert ? -1 : 1);

// Absolute.
if ($diff->days > 30) {
return $date->format('M Y');
}

// Relative.
switch ($diff->days) {
switch ($days) {
case -1:
return 'yesterday';

case 0:
return 'today';

case 1:
return 'yesterday';
return 'tomorrow';
}

return "$diff->days days";
Expand Down
10 changes: 10 additions & 0 deletions test/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,14 @@ public function test31DaysAgo(): void
{
self::assertSame(($date = new \DateTime('-31 day'))->format('M Y'), Date::adapt($date));
}

public function testTomorrow(): void
{
self::assertSame('tomorrow', Date::adapt(new \DateTime('1 day')));
}

public function testDayAfterTomorrow(): void
{
self::assertSame('2 days', Date::adapt(new \DateTime('2 day')));
}
}

0 comments on commit 63564d6

Please sign in to comment.