Added a minimum age feature to the date formatting. It's not really a perfect solution, but it's working for our needs at the moment.
git-svn-id: file:///svn-source/pmgr/branches/yafr_20090716/site@417 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -41,13 +41,16 @@ class FormatHelper extends AppHelper {
|
||||
$date_fmt = 'm/d/Y';
|
||||
return (self::$time->format($date_fmt, $date) .
|
||||
($age
|
||||
? ' (' . self::age($date) . ')'
|
||||
? ' (' . self::age($date, 60*60*24) . ')'
|
||||
: ''));
|
||||
}
|
||||
|
||||
function datetime($datetime) {
|
||||
function datetime($datetime, $age = false) {
|
||||
if (!$datetime) return null;
|
||||
return self::$time->nice($datetime);
|
||||
return (self::$time->nice($datetime) .
|
||||
($age
|
||||
? ' (' . self::age($datetime) . ')'
|
||||
: ''));
|
||||
}
|
||||
|
||||
function phone($phone, $ext = null) {
|
||||
@@ -78,7 +81,7 @@ class FormatHelper extends AppHelper {
|
||||
return $comment;
|
||||
}
|
||||
|
||||
function age($datetime) {
|
||||
function age($datetime, $min_span = 0) {
|
||||
if (!isset($datetime))
|
||||
return null;
|
||||
|
||||
@@ -90,52 +93,81 @@ class FormatHelper extends AppHelper {
|
||||
$timeto = $backwards ? $seconds : $now;
|
||||
$span = $timeto - $timefrom;
|
||||
|
||||
// Display seconds if under 45 seconds
|
||||
if ($span === 0) {
|
||||
//pr(compact('now', 'seconds', 'backwards', 'timefrom', 'timeto', 'span', 'min_span'));
|
||||
|
||||
// If now, just return so
|
||||
if ($span === 0)
|
||||
return __('now', true);
|
||||
}
|
||||
if ($span < 45) {
|
||||
|
||||
// Display seconds if under 45 seconds
|
||||
if ($span < 45 && $span >= $min_span) {
|
||||
$approx = round($span);
|
||||
$unit = 'second';
|
||||
}
|
||||
|
||||
// Display minutes if under 45 minutes
|
||||
elseif (($span /= 60) < 45) {
|
||||
$approx = round($span);
|
||||
if (!isset($approx)) {
|
||||
$unit = 'minute';
|
||||
$span /= 60; $min_span /= 60;
|
||||
if ($span < 45 && ($span >= $min_span || $min_span <= 1))
|
||||
$approx = round($span);
|
||||
}
|
||||
// Display hours if under 18 hours
|
||||
elseif (($span /= 60) < 18) {
|
||||
$approx = round($span);
|
||||
$unit = 'hour';
|
||||
}
|
||||
// Display days if under 6.5 days
|
||||
elseif (($span /= 24) < 6.5) {
|
||||
$approx = round($span);
|
||||
$unit = 'day';
|
||||
}
|
||||
// Display weeks if less than 8 weeks
|
||||
elseif (($span /= 7) < 8) {
|
||||
$approx = round($span);
|
||||
$unit = 'week';
|
||||
}
|
||||
// Display months if less than 20 months
|
||||
elseif (($span /= (365.2425 / (7 * 12))) < 20) {
|
||||
$approx = round($span);
|
||||
$unit = 'month';
|
||||
|
||||
// Months are from 28-31 days. If it's too
|
||||
// close to being an exact month, just fudge
|
||||
// by saying the result is 'about' N months
|
||||
// instead of 'almost' or 'over' N months,
|
||||
// since we can't be accurate on this without
|
||||
// taking into account the day of the week.
|
||||
if ((abs($span - $approx) * (365.2425 / 12)) < 3)
|
||||
$relative = 'about';
|
||||
// Display hours if under 18 hours
|
||||
if (!isset($approx)) {
|
||||
$unit = 'hour';
|
||||
$span /= 60; $min_span /= 60;
|
||||
if ($span < 18 && ($span >= $min_span || $min_span <= 1))
|
||||
$approx = round($span);
|
||||
}
|
||||
else {
|
||||
$span /= 12;
|
||||
$approx = round($span);
|
||||
|
||||
// Display days if under 6.5 days
|
||||
if (!isset($approx)) {
|
||||
$unit = 'day';
|
||||
$span /= 24; $min_span /= 24;
|
||||
if ($span < 6.5 && ($span >= $min_span || $min_span <= 1))
|
||||
$approx = round($span);
|
||||
}
|
||||
|
||||
// Display weeks if less than 8 weeks
|
||||
if (!isset($approx)) {
|
||||
$unit = 'week';
|
||||
$span /= 7; $min_span /= 7;
|
||||
if ($span < 8 && ($span >= $min_span || $min_span <= 1))
|
||||
$approx = round($span);
|
||||
}
|
||||
|
||||
// Display months if less than 20 months
|
||||
if (!isset($approx)) {
|
||||
$unit = 'month';
|
||||
$span /= 365.2425 / (7*12); $min_span /= 365.2425 / (7*12);
|
||||
if ($span < 20 && ($span >= $min_span || $min_span <= 1)) {
|
||||
$approx = round($span);
|
||||
// Months are from 28-31 days. If it's too
|
||||
// close to being an exact month, just fudge
|
||||
// by saying the result is 'about' N months
|
||||
// instead of 'almost' or 'over' N months,
|
||||
// since we can't be accurate on this without
|
||||
// taking into account the day of the week.
|
||||
if ((abs($span - $approx) * (365.2425 / 12)) < 3)
|
||||
$relative = 'about';
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, just display years
|
||||
if (!isset($approx)) {
|
||||
$unit = 'year';
|
||||
$span /= 12; $min_span /= 12;
|
||||
$approx = round($span);
|
||||
}
|
||||
|
||||
//pr(compact('span', 'min_span', 'approx', 'unit'));
|
||||
|
||||
if ($approx == 0) {
|
||||
if ($unit == 'day')
|
||||
return __('today', true);
|
||||
|
||||
return __('this ' . $unit, true);
|
||||
}
|
||||
|
||||
return (__(isset($relative)
|
||||
|
||||
Reference in New Issue
Block a user