mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Making CakeTime display 'about X ago' when time passed is lower than accuracy
Removing unnecessary else Also support 'about' times for future times Make 'about' times translateable in all languages
This commit is contained in:
parent
1bdcc58e31
commit
1cb41294b8
2 changed files with 70 additions and 16 deletions
|
@ -227,6 +227,20 @@ class CakeTimeTest extends CakeTestCase {
|
||||||
);
|
);
|
||||||
$expected = '1 year';
|
$expected = '1 year';
|
||||||
$this->assertEquals($expected, $result);
|
$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')
|
array('end' => '2 years')
|
||||||
);
|
);
|
||||||
$this->assertEquals('1 year, 1 month, 5 days ago', $result);
|
$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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -820,48 +820,76 @@ class CakeTime {
|
||||||
return __d('cake', 'on %s', date($format, $inSeconds));
|
return __d('cake', 'on %s', date($format, $inSeconds));
|
||||||
}
|
}
|
||||||
|
|
||||||
$f = $accuracy['second'];
|
$fWord = $accuracy['second'];
|
||||||
if ($years > 0) {
|
if ($years > 0) {
|
||||||
$f = $accuracy['year'];
|
$fWord = $accuracy['year'];
|
||||||
} elseif (abs($months) > 0) {
|
} elseif (abs($months) > 0) {
|
||||||
$f = $accuracy['month'];
|
$fWord = $accuracy['month'];
|
||||||
} elseif (abs($weeks) > 0) {
|
} elseif (abs($weeks) > 0) {
|
||||||
$f = $accuracy['week'];
|
$fWord = $accuracy['week'];
|
||||||
} elseif (abs($days) > 0) {
|
} elseif (abs($days) > 0) {
|
||||||
$f = $accuracy['day'];
|
$fWord = $accuracy['day'];
|
||||||
} elseif (abs($hours) > 0) {
|
} elseif (abs($hours) > 0) {
|
||||||
$f = $accuracy['hour'];
|
$fWord = $accuracy['hour'];
|
||||||
} elseif (abs($minutes) > 0) {
|
} 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 = '';
|
$relativeDate = '';
|
||||||
if ($f >= 1 && $years > 0) {
|
if ($fNum >= 1 && $years > 0) {
|
||||||
$relativeDate .= ($relativeDate ? ', ' : '') . __dn('cake', '%d year', '%d years', $years, $years);
|
$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);
|
$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);
|
$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);
|
$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);
|
$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);
|
$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);
|
$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) {
|
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;
|
return $relativeDate;
|
||||||
|
|
Loading…
Reference in a new issue