Added round up to Time::ago

Some strings were resulting in returns of "24 hours" and "7 days" that needed to be rounded up to the next increment. Closes #28
This commit is contained in:
Joshua Sherman 2014-02-02 09:47:24 -05:00
parent 2559672246
commit 4c59f7c55b
2 changed files with 119 additions and 31 deletions

View file

@ -176,40 +176,80 @@ class Time
$time_ago = 'seconds'; $time_ago = 'seconds';
} }
// Less than 1 hour ago (minutes ago) // Less than 1 hour ago (minutes ago)
elseif ($difference < 3600) elseif ($difference < Time::HOUR)
{ {
$minutes = round($difference / 60); $minutes = round($difference / 60);
$time_ago = $minutes . ' minute' . ($minutes != 1 ? 's' : '');
if ($minutes == 60)
{
$time_ago = 'an hour';
}
else
{
$time_ago = ($minutes == 1 ? 'a' : $minutes) . ' minute' . ($minutes != 1 ? 's' : '');
}
} }
// Less than 1 day ago (hours ago) // Less than 1 day ago (hours ago)
elseif ($difference < 86400) elseif ($difference < Time::DAY)
{ {
$hours = round($difference / 3600); $hours = round($difference / Time::HOUR);
$time_ago = $hours . ' hour' . ($hours != 1 ? 's' : '');
if ($hours == 24)
{
$time_ago = 'a day';
}
else
{
$time_ago = ($hours == 1 ? 'an' : $hours) . ' hour' . ($hours != 1 ? 's' : '');
}
} }
// Less than 1 week ago (days ago) // Less than 1 week ago (days ago)
elseif ($difference < 604800) elseif ($difference < Time::WEEK)
{ {
$days = round($difference / 86400); $days = round($difference / Time::DAY);
$time_ago = $days . ' day' . ($days != 1 ? 's' : '');
if ($days == 7)
{
$time_ago = 'a week';
}
else
{
$time_ago = ($days == 1 ? 'a' : $days) . ' day' . ($days != 1 ? 's' : '');
}
} }
// Less than 1 month ago (weeks ago) // Less than 1 month ago (weeks ago)
elseif ($difference < 2419200) elseif ($difference < Time::MONTH)
{ {
$weeks = round($difference / 604800); $weeks = round($difference / Time::WEEK);
$time_ago = $weeks . ' week' . ($weeks != 1 ? 's' : '');
if ($weeks == 4)
{
$time_ago = 'a month';
}
else
{
$time_ago = ($weeks == 1 ? 'a' : $weeks) . ' week' . ($weeks != 1 ? 's' : '');
}
} }
// Less than 1 year ago (months ago) // Less than 1 year ago (months ago)
elseif ($difference < 31449600) elseif ($difference < Time::YEAR)
{ {
$months = round($difference / 2419200); $months = round($difference / Time::MONTH);
$time_ago = $months . ' month' . ($months != 1 ? 's' : '');
if ($months == 12)
{
$time_ago = 'a year';
}
else
{
$time_ago = ($months == 1 ? 'a' : $months) . ' month' . ($months != 1 ? 's' : '');
}
} }
// Over 1 year ago (years ago) // Over 1 year ago (years ago)
else else
{ {
$years = round($difference / 31449600); $years = round($difference / Time::YEAR);
$time_ago = $years . ' year' . ($years != 1 ? 's' : ''); $time_ago = ($years == 1 ? 'a' : $years) . ' year' . ($years != 1 ? 's' : '');
} }
$time_ago .= $suffix; $time_ago .= $suffix;

View file

@ -32,41 +32,64 @@ class TimeTest extends PHPUnit_Framework_TestCase
$this->assertEquals('seconds ago', Time::ago(strtotime('-30 seconds'))); $this->assertEquals('seconds ago', Time::ago(strtotime('-30 seconds')));
} }
public function testAgoPastTimeMinute()
{
$this->assertEquals('a minute ago', Time::ago(strtotime('-1 minutes')));
}
public function testAgoPastTimeMinutes() public function testAgoPastTimeMinutes()
{ {
$this->assertEquals('5 minutes ago', Time::ago(strtotime('-5 minutes'))); $this->assertEquals('5 minutes ago', Time::ago(strtotime('-5 minutes')));
} }
public function testAgoPastTimeHour()
{
$this->assertEquals('an hour ago', Time::ago(strtotime('-1 hours')));
}
public function testAgoPastTimeHours() public function testAgoPastTimeHours()
{ {
$this->assertEquals('1 hour ago', Time::ago(strtotime('-1 hour'))); $this->assertEquals('2 hours ago', Time::ago(strtotime('-2 hours')));
}
public function testAgoPastTimeDay()
{
$this->assertEquals('a day ago', Time::ago(strtotime('-1 days')));
} }
public function testAgoPastTimeDays() public function testAgoPastTimeDays()
{ {
$this->assertEquals('1 day ago', Time::ago(strtotime('-1 day'))); $this->assertEquals('2 days ago', Time::ago(strtotime('-2 days')));
} }
/* @todo Need to fix these results so it doesn't fail. public function testAgoPastTimeWeek()
public function testAgoPastTimeDays2()
{ {
$this->assertEquals('1 day ago', Time::ago(strtotime('-23 hours -55 minutes'))); $this->assertEquals('a week ago', Time::ago(strtotime('-1 weeks')));
} }
*/
public function testAgoPastTimeWeeks() public function testAgoPastTimeWeeks()
{ {
$this->assertEquals('1 week ago', Time::ago(strtotime('-1 week'))); $this->assertEquals('2 weeks ago', Time::ago(strtotime('-2 weeks')));
}
public function testAgoPastTimeMonth()
{
$this->assertEquals('a month ago', Time::ago(strtotime('-1 months')));
} }
public function testAgoPastTimeMonths() public function testAgoPastTimeMonths()
{ {
$this->assertEquals('1 month ago', Time::ago(strtotime('-1 month'))); $this->assertEquals('2 months ago', Time::ago(strtotime('-2 months')));
}
public function testAgoPastTimeYear()
{
$this->assertEquals('a year ago', Time::ago(strtotime('-1 years')));
} }
public function testAgoPastTimeYears() public function testAgoPastTimeYears()
{ {
$this->assertEquals('1 year ago', Time::ago(strtotime('-1 year'))); $this->assertEquals('2 years ago', Time::ago(strtotime('-2 years')));
} }
public function testAgoFutureTimeSeconds() public function testAgoFutureTimeSeconds()
@ -81,33 +104,58 @@ class TimeTest extends PHPUnit_Framework_TestCase
public function testAgoFutureTimeHours() public function testAgoFutureTimeHours()
{ {
$this->assertEquals('1 hour from now', Time::ago(strtotime('+1 hour'))); $this->assertEquals('an hour from now', Time::ago(strtotime('+1 hour')));
} }
public function testAgoFutureTimeDays() public function testAgoFutureTimeDays()
{ {
$this->assertEquals('1 day from now', Time::ago(strtotime('+1 day'))); $this->assertEquals('a day from now', Time::ago(strtotime('+1 day')));
} }
public function testAgoFutureTimeWeeks() public function testAgoFutureTimeWeeks()
{ {
$this->assertEquals('1 week from now', Time::ago(strtotime('+1 week'))); $this->assertEquals('a week from now', Time::ago(strtotime('+1 week')));
} }
public function testAgoFutureTimeMonths() public function testAgoFutureTimeMonths()
{ {
$this->assertEquals('1 month from now', Time::ago(strtotime('+1 month'))); $this->assertEquals('a month from now', Time::ago(strtotime('+1 month')));
} }
public function testAgoFutureTimeYears() public function testAgoFutureTimeYears()
{ {
$this->assertEquals('1 year from now', Time::ago(strtotime('+1 year'))); $this->assertEquals('a year from now', Time::ago(strtotime('+1 year')));
} }
public function testTimestamp() public function testTimestamp()
{ {
$this->assertEquals(gmdate('Y-m-d H:i:s'), Time::timestamp()); $this->assertEquals(gmdate('Y-m-d H:i:s'), Time::timestamp());
} }
public function testRoundUpHour()
{
$this->assertEquals('an hour ago', Time::ago(strtotime('-59 minutes -55 seconds')));
}
public function testRoundUpDay()
{
$this->assertEquals('a day ago', Time::ago(strtotime('-23 hours -55 minutes')));
}
public function testRoundUpWeek()
{
$this->assertEquals('a week ago', Time::ago(strtotime('-6 days -23 hours')));
}
public function testRoundUpMonth()
{
$this->assertEquals('a month ago', Time::ago(strtotime('-29 days')));
}
public function testRoundUpYear()
{
$this->assertEquals('a year ago', Time::ago(strtotime('-364 days')));
}
} }
?> ?>