Permalink
Browse files

Add support for times in the future.

  • Loading branch information...
1 parent 672a251 commit 771ff8713821c51a4ba1ba7dedf29cc40a830425 @kenguest kenguest committed Mar 1, 2013
Showing with 62 additions and 9 deletions.
  1. +2 −0 README.rst
  2. +11 −1 examples/human.php
  3. +35 −8 src/Date/HumanDiff.php
  4. +14 −0 tests/Date/HumanDiffTest.php
View
@@ -26,6 +26,8 @@ Usage
require_once 'Date/HumanDiff.php';
$dh = new Date_HumanDiff();
echo $dh->get(time() - 3600);//shows "an hour ago"
+ echo $dh->get(time() + 3600 * 24, time()); //shows "tomorrow"
+
?>
View
@@ -1,5 +1,5 @@
<?php
-require_once 'Date/HumanDiff.php';
+require_once '../src/Date/HumanDiff.php';
$dh = new Date_HumanDiff();
@@ -8,4 +8,14 @@
echo $dh->get(-3600, 0) . "\n"; //shows "an hour ago"
+echo $dh->get(time() + 1800, time()) . "\n"; //shows "30 minutes away"
+echo $dh->get(time() + 3600, time()) . "\n"; //shows "an hour away"
+echo $dh->get(time() + 7200, time()) . "\n"; //shows "2 hours away"
+echo $dh->get(time() + 3600 * 24, time()) . "\n"; //shows "tomorrow"
+echo $dh->get(time() + 3600 * 48, time()) . "\n"; //shows "2 days away"
+echo $dh->get(time() + 3600 * 96, time()) . "\n"; //shows "4 days away"
+echo $dh->get(time() + 3600 * 24 * 7, time()) . "\n"; //shows "a week away"
+echo $dh->get(time() + 3600 * 24 * 7 * 4, time()) . "\n"; //shows "4 weeks away"
+echo $dh->get(time() + 3600 * 24 * 7 * 5, time()) . "\n"; //shows "a month away"
+echo $dh->get(time() + 3600 * 24 * 7 * 8, time()) . "\n"; //shows "2 months away"
?>
View
@@ -79,6 +79,21 @@ public function __construct()
array(1.5 * static::$YEAR, 'a year ago', 1),
array(PHP_INT_MAX, '%d years ago', static::$YEAR)
);
+ $this->futureformats = array(
+ array(0.7 * static::$MINUTE, 'in just a moment', -1),
+ array(1.5 * static::$MINUTE, 'a minute away', -1),
+ array( 60 * static::$MINUTE, '%d minutes away', -static::$MINUTE),
+ array(1.5 * static::$HOUR, 'an hour away', -1),
+ array( static::$DAY, '%d hours away', -static::$HOUR),
+ array( 2 * static::$DAY, 'tomorrow', -1),
+ array( 7 * static::$DAY, '%d days away', -static::$DAY),
+ array(1.5 * static::$WEEK, 'a week away', -1),
+ array( static::$MONTH, '%d weeks away', -static::$WEEK),
+ array(1.5 * static::$MONTH, 'a month away', -1),
+ array( static::$YEAR, '%d months away', -static::$MONTH),
+ array(1.5 * static::$YEAR, 'a year away', -1),
+ array(PHP_INT_MAX, '%d years away', -static::$YEAR)
+ );
}
/**
@@ -101,14 +116,26 @@ public function get($timestamp, $reference = null)
$delta = $reference - $timestamp;
- foreach ($this->formats as $format) {
- if ($delta < $format[0]) {
- return $this->getTranslation(
- $format[1],
- round($delta / $format[2])
- );
- }
- };
+ if ($delta >= 0) {
+ foreach ($this->formats as $format) {
+ if ($delta < $format[0]) {
+ return $this->getTranslation(
+ $format[1],
+ round($delta / $format[2])
+ );
+ }
+ };
+ } else {
+ foreach ($this->futureformats as $format) {
+ if (-$delta < $format[0]) {
+ return $this->getTranslation(
+ $format[1],
+ round($delta / $format[2])
+ );
+ }
+ };
+
+ }
}
/**
@@ -13,19 +13,26 @@ public function testGetJustNow()
{
$this->assertEquals('just now', $this->dh->get(100, 100));
$this->assertEquals('just now', $this->dh->get(time() - 10));
+ $this->assertEquals('in just a moment', $this->dh->get(time() + 10));
}
public function testGetAMinute()
{
$this->assertEquals('a minute ago', $this->dh->get(40, 100));
$this->assertEquals('a minute ago', $this->dh->get(time() - 60));
$this->assertEquals('a minute ago', $this->dh->get(time() - 50));
+ // one minute in the future
+ $this->assertEquals('a minute away', $this->dh->get(time() + 50));
+ $this->assertEquals('a minute away', $this->dh->get(time() + 60));
+ $this->assertEquals('a minute away', $this->dh->get(time() + 61));
}
public function testGetMinutes()
{
$this->assertEquals('2 minutes ago', $this->dh->get(-120, 0));
$this->assertEquals('40 minutes ago', $this->dh->get(time() - 60 * 40));
+ $this->assertEquals('2 minutes away', $this->dh->get(time() + 90));
+ $this->assertEquals('40 minutes away', $this->dh->get(time() + 60 * 40));
}
public function testGetYesterday()
@@ -34,15 +41,22 @@ public function testGetYesterday()
$this->assertEquals('yesterday', $this->dh->get(time() - 86400));
}
+ public function testGetTomorrow()
+ {
+ $this->assertEquals('tomorrow', $this->dh->get(time() + 86400));
+ }
+
public function testGetDays()
{
$this->assertEquals('2 days ago', $this->dh->get(-86400 * 2, 0));
$this->assertEquals('6 days ago', $this->dh->get(time() - 86400 * 6));
+ $this->assertEquals('2 days away', $this->dh->get(time() + 86400 * 2));
}
public function testGetAWeek()
{
$this->assertEquals('a week ago', $this->dh->get(-86400 * 7, 0));
+ $this->assertEquals('a week away', $this->dh->get(time() + 86400 * 7));
}
public function testGetWeeks()

0 comments on commit 771ff87

Please sign in to comment.