Skip to content

Commit

Permalink
Implement methods from, fromNow, to, toNow
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Jul 11, 2018
1 parent 4c97613 commit 8a93dbe
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 1 deletion.
98 changes: 97 additions & 1 deletion src/Carbon/Traits/Difference.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ public function secondsUntilEndOfDay()
}

/**
* Get the difference in a human readable format in the current locale.
* Get the difference in a human readable format in the current locale from current instance to an other
* instance given (or now if null given).
*
* When comparing a value in the past to default now:
* 1 hour ago
Expand Down Expand Up @@ -434,4 +435,99 @@ public function diffForHumans($other = null, $syntax = null, $short = false, $pa

return static::translator()->trans($transId, [':time' => $time]);
}

/**
* @alias diffForHumans
*
* Get the difference in a human readable format in the current locale from current instance to an other
* instance given (or now if null given).
*/
public function from($other = null, $syntax = null, $short = false, $parts = 1)
{
return $this->diffForHumans($other, $syntax, $short, $parts);
}

/**
* Get the difference in a human readable format in the current locale from an other
* instance given (or now if null given) to current instance.
*
* When comparing a value in the past to default now:
* 1 hour from now
* 5 months from now
*
* When comparing a value in the future to default now:
* 1 hour ago
* 5 months ago
*
* When comparing a value in the past to another value:
* 1 hour after
* 5 months after
*
* When comparing a value in the future to another value:
* 1 hour before
* 5 months before
*
* @param Carbon|null $other
* @param int $syntax difference modifiers (ago, after, etc) rules
* Possible values:
* - CarbonInterface::DIFF_ABSOLUTE
* - CarbonInterface::DIFF_RELATIVE_AUTO
* - CarbonInterface::DIFF_RELATIVE_TO_NOW
* - CarbonInterface::DIFF_RELATIVE_TO_OTHER
* Default value: CarbonInterface::DIFF_RELATIVE_AUTO
* @param bool $short displays short format of time units
* @param int $parts displays number of parts in the interval
*
* @return string
*/
public function to($other = null, $syntax = null, $short = false, $parts = 1)
{
if (!$syntax && !$other) {
$syntax = CarbonInterface::DIFF_RELATIVE_TO_NOW;
}

return $this->resolveCarbon($other)->diffForHumans($this, $syntax, $short, $parts);
}

/**
* Get the difference in a human readable format in the current locale from current
* instance to now.
*
* @param int $syntax difference modifiers (ago, after, etc) rules
* Possible values:
* - CarbonInterface::DIFF_ABSOLUTE
* - CarbonInterface::DIFF_RELATIVE_AUTO
* - CarbonInterface::DIFF_RELATIVE_TO_NOW
* - CarbonInterface::DIFF_RELATIVE_TO_OTHER
* Default value: CarbonInterface::DIFF_RELATIVE_AUTO
* @param bool $short displays short format of time units
* @param int $parts displays number of parts in the interval
*
* @return string
*/
public function fromNow($syntax = null, $short = false, $parts = 1)
{
return $this->from(null, $syntax, $short, $parts);
}

/**
* Get the difference in a human readable format in the current locale from an other
* instance given to now
*
* @param int $syntax difference modifiers (ago, after, etc) rules
* Possible values:
* - CarbonInterface::DIFF_ABSOLUTE
* - CarbonInterface::DIFF_RELATIVE_AUTO
* - CarbonInterface::DIFF_RELATIVE_TO_NOW
* - CarbonInterface::DIFF_RELATIVE_TO_OTHER
* Default value: CarbonInterface::DIFF_RELATIVE_AUTO
* @param bool $short displays short format of time units
* @param int $parts displays number of parts in the interval
*
* @return string
*/
public function toNow($syntax = null, $short = false, $parts = 1)
{
return $this->to(null, $syntax, $short, $parts);
}
}
52 changes: 52 additions & 0 deletions tests/Carbon/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,58 @@ public function testDiffOptions()
Carbon::setHumanDiffOptions($options);
}

public function testFromNow()
{
Carbon::setLocale('en');
$this->assertSame('2 days from now', Carbon::now()->addDays(2)->fromNow());
Carbon::setLocale('fr');
$this->assertSame('dans 2 jours', Carbon::now()->addDays(2)->fromNow());
Carbon::setLocale('en');
$this->assertSame('2 days after', Carbon::now()->addDays(2)->fromNow(CarbonInterface::DIFF_RELATIVE_TO_OTHER));
$this->assertSame('2d from now', Carbon::now()->addDays(2)->addHours(5)->fromNow(null, true));
$this->assertSame('2 days 5 hours', Carbon::now()->addDays(2)->addHours(5)->fromNow(true, false, 2));
}

public function testFrom()
{
Carbon::setLocale('en');
$this->assertSame('2 days from now', Carbon::now()->addDays(2)->from());
$this->assertSame('2 days from now', Carbon::now()->addDays(2)->from(null));
$this->assertSame('2 days after', Carbon::now()->addDay()->from(Carbon::now()->subDay()));
Carbon::setLocale('fr');
$this->assertSame('2 jours après', Carbon::now()->addDay()->from(Carbon::now()->subDay()));
Carbon::setLocale('en');
$this->assertSame('2 days from now', Carbon::now()->addDay()->from(Carbon::now()->subDay(), CarbonInterface::DIFF_RELATIVE_TO_NOW));
$this->assertSame('2d after', Carbon::now()->addDay()->addHours(5)->from(Carbon::now()->subDay(), null, true));
$this->assertSame('2 days 5 hours', Carbon::now()->addDay()->addHours(5)->from(Carbon::now()->subDay(), true, false, 2));
}

public function testToNow()
{
Carbon::setLocale('en');
$this->assertSame('2 days ago', Carbon::now()->addDays(2)->toNow());
Carbon::setLocale('fr');
$this->assertSame('il y a 2 jours', Carbon::now()->addDays(2)->toNow());
Carbon::setLocale('en');
$this->assertSame('2 days before', Carbon::now()->addDays(2)->toNow(CarbonInterface::DIFF_RELATIVE_TO_OTHER));
$this->assertSame('2d ago', Carbon::now()->addDays(2)->addHours(5)->toNow(null, true));
$this->assertSame('2 days 5 hours', Carbon::now()->addDays(2)->addHours(5)->toNow(true, false, 2));
}

public function testTo()
{
Carbon::setLocale('en');
$this->assertSame('2 days ago', Carbon::now()->addDays(2)->to());
$this->assertSame('2 days ago', Carbon::now()->addDays(2)->to(null));
$this->assertSame('2 days before', Carbon::now()->addDay()->to(Carbon::now()->subDay()));
Carbon::setLocale('fr');
$this->assertSame('2 jours avant', Carbon::now()->addDay()->to(Carbon::now()->subDay()));
Carbon::setLocale('en');
$this->assertSame('2 days ago', Carbon::now()->addDay()->to(Carbon::now()->subDay(), CarbonInterface::DIFF_RELATIVE_TO_NOW));
$this->assertSame('2d before', Carbon::now()->addDay()->addHours(5)->to(Carbon::now()->subDay(), null, true));
$this->assertSame('2 days 5 hours', Carbon::now()->addDay()->addHours(5)->to(Carbon::now()->subDay(), true, false, 2));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Expected null, string, DateTime or DateTimeInterface, integer given
Expand Down

0 comments on commit 8a93dbe

Please sign in to comment.