diff --git a/lib/Cake/Test/Case/Utility/CakeTimeTest.php b/lib/Cake/Test/Case/Utility/CakeTimeTest.php index ed3ac97e1..028723b93 100644 --- a/lib/Cake/Test/Case/Utility/CakeTimeTest.php +++ b/lib/Cake/Test/Case/Utility/CakeTimeTest.php @@ -227,6 +227,20 @@ class CakeTimeTest extends CakeTestCase { ); $expected = '1 year'; $this->assertEquals($expected, $result); + + $result = $this->Time->timeAgoInWords( + strtotime('+58 minutes'), + array('accuracy' => 'hour') + ); + $expected = 'in about an hour'; + $this->assertEquals($expected, $result); + + $result = $this->Time->timeAgoInWords( + strtotime('+23 hours'), + array('accuracy' => 'day') + ); + $expected = 'in about a day'; + $this->assertEquals($expected, $result); } /** @@ -314,6 +328,18 @@ class CakeTimeTest extends CakeTestCase { array('end' => '2 years') ); $this->assertEquals('1 year, 1 month, 5 days ago', $result); + + $result = $this->Time->timeAgoInWords( + strtotime('-58 minutes'), + array('accuracy' => 'hour') + ); + $this->assertEquals('about an hour ago', $result); + + $result = $this->Time->timeAgoInWords( + strtotime('-23 hours'), + array('accuracy' => 'day') + ); + $this->assertEquals('about a day ago', $result); } /** diff --git a/lib/Cake/Utility/CakeTime.php b/lib/Cake/Utility/CakeTime.php index 0804f1644..635d0612b 100644 --- a/lib/Cake/Utility/CakeTime.php +++ b/lib/Cake/Utility/CakeTime.php @@ -820,48 +820,76 @@ class CakeTime { return __d('cake', 'on %s', date($format, $inSeconds)); } - $f = $accuracy['second']; + $fWord = $accuracy['second']; if ($years > 0) { - $f = $accuracy['year']; + $fWord = $accuracy['year']; } elseif (abs($months) > 0) { - $f = $accuracy['month']; + $fWord = $accuracy['month']; } elseif (abs($weeks) > 0) { - $f = $accuracy['week']; + $fWord = $accuracy['week']; } elseif (abs($days) > 0) { - $f = $accuracy['day']; + $fWord = $accuracy['day']; } elseif (abs($hours) > 0) { - $f = $accuracy['hour']; + $fWord = $accuracy['hour']; } elseif (abs($minutes) > 0) { - $f = $accuracy['minute']; + $fWord = $accuracy['minute']; } - $f = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $f); + $fNum = str_replace(array('year', 'month', 'week', 'day', 'hour', 'minute', 'second'), array(1, 2, 3, 4, 5, 6, 7), $fWord); $relativeDate = ''; - if ($f >= 1 && $years > 0) { + if ($fNum >= 1 && $years > 0) { $relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d year', '%d years', $years, $years); } - if ($f >= 2 && $months > 0) { + if ($fNum >= 2 && $months > 0) { $relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d month', '%d months', $months, $months); } - if ($f >= 3 && $weeks > 0) { + if ($fNum >= 3 && $weeks > 0) { $relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d week', '%d weeks', $weeks, $weeks); } - if ($f >= 4 && $days > 0) { + if ($fNum >= 4 && $days > 0) { $relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d day', '%d days', $days, $days); } - if ($f >= 5 && $hours > 0) { + if ($fNum >= 5 && $hours > 0) { $relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d hour', '%d hours', $hours, $hours); } - if ($f >= 6 && $minutes > 0) { + if ($fNum >= 6 && $minutes > 0) { $relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d minute', '%d minutes', $minutes, $minutes); } - if ($f >= 7 && $seconds > 0) { + if ($fNum >= 7 && $seconds > 0) { $relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d second', '%d seconds', $seconds, $seconds); } + $aboutAgo = array( + 'second' => __d('cake', 'about a second ago'), + 'minute' => __d('cake', 'about a minute ago'), + 'hour' => __d('cake', 'about an hour ago'), + 'day' => __d('cake', 'about a day ago'), + 'week' => __d('cake', 'about a week ago'), + 'year' => __d('cake', 'about a year ago') + ); + + $aboutIn = array( + 'second' => __d('cake', 'in about a second'), + 'minute' => __d('cake', 'in about a minute'), + 'hour' => __d('cake', 'in about an hour'), + 'day' => __d('cake', 'in about a day'), + 'week' => __d('cake', 'in about a week'), + 'year' => __d('cake', 'in about a year') + ); + + // When time has passed if (!$backwards) { - return __d('cake', '%s ago', $relativeDate); + if ($relativeDate) { + return __d('cake', '%s ago', $relativeDate); + } + + return $aboutAgo[$fWord]; + } + + // When time is to come + if (!$relativeDate) { + return $aboutIn[$fWord]; } return $relativeDate;