Customized our fuzzy 'age' function to suit our purpose
git-svn-id: file:///svn-source/pmgr/branches/ledger_transactions_20090605/site@90 97e9348a-65ac-dc4b-aefc-98561f571b83
This commit is contained in:
@@ -11,6 +11,14 @@ class FormatHelper extends AppHelper {
|
||||
self::$number = new NumberHelper;
|
||||
}
|
||||
|
||||
function _n($amount, $singular, $plural = null) {
|
||||
return ($amount . ' ' .
|
||||
__n($singular,
|
||||
$plural ? $plural : $singular.'s',
|
||||
$amount,
|
||||
true));
|
||||
}
|
||||
|
||||
function currency($amount) {
|
||||
if (!isset($amount))
|
||||
return '-';
|
||||
@@ -19,19 +27,16 @@ class FormatHelper extends AppHelper {
|
||||
return (isset($amount)
|
||||
? self::$number->currency($amount)
|
||||
: null);
|
||||
|
||||
/* if ($money < 0) */
|
||||
/* return "($ " . number_format(-1*$money, 2) . ")"; */
|
||||
/* else */
|
||||
/* return "$ " . number_format($money, 2); */
|
||||
}
|
||||
|
||||
function date($date) {
|
||||
function date($date, $age = false) {
|
||||
if (!$date) return null;
|
||||
|
||||
$date_fmt = 'm/d/Y';
|
||||
return (isset($date)
|
||||
? self::$time->format($date_fmt, $date)
|
||||
//? date_format(date_create($date), $date_fmt)
|
||||
: null);
|
||||
return (self::$time->format($date_fmt, $date) .
|
||||
($age
|
||||
? ' (' . self::age($date) . ')'
|
||||
: ''));
|
||||
}
|
||||
|
||||
function datetime($datetime) {
|
||||
@@ -65,8 +70,119 @@ class FormatHelper extends AppHelper {
|
||||
}
|
||||
|
||||
function age($datetime) {
|
||||
return $time->timeAgoInWords($datetime,
|
||||
array('end' => '+99 years'));
|
||||
if (!isset($datetime))
|
||||
return null;
|
||||
|
||||
$now = time();
|
||||
$seconds = self::$time->fromString($datetime);
|
||||
$backwards = ($seconds > $now);
|
||||
|
||||
$timefrom = $backwards ? $now : $seconds;
|
||||
$timeto = $backwards ? $seconds : $now;
|
||||
$span = $timeto - $timefrom;
|
||||
|
||||
// Display seconds if under 45 seconds
|
||||
if ($span === 0) {
|
||||
return __('now', true);
|
||||
}
|
||||
if ($span < 45) {
|
||||
$approx = round($span);
|
||||
$unit = 'second';
|
||||
}
|
||||
// Display minutes if under 45 minutes
|
||||
elseif (($span /= 60) < 45) {
|
||||
$approx = round($span);
|
||||
$unit = 'minute';
|
||||
}
|
||||
// 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';
|
||||
}
|
||||
else {
|
||||
$span /= 12;
|
||||
$approx = round($span);
|
||||
$unit = 'year';
|
||||
}
|
||||
|
||||
return (__(isset($relative)
|
||||
? $relative
|
||||
: ($approx == $span
|
||||
? ''
|
||||
: ($approx > $span ? 'almost' : 'over')), true)
|
||||
. ' '
|
||||
. self::_n($approx, $unit)
|
||||
. ($backwards ? '' : __(' ago', true)));
|
||||
}
|
||||
|
||||
/*****************************
|
||||
** Test code
|
||||
**/
|
||||
/* function basevals($max) { */
|
||||
/* return incrvals($max); */
|
||||
/* if ($max % 2) { */
|
||||
/* return array(0, 1, ($max+1)/2-1, $max/2, ($max+1)/2, $max-1); */
|
||||
/* } */
|
||||
/* else { */
|
||||
/* return array(0, 1, $max/2-1, $max/2, $max/2+1, $max-1); */
|
||||
/* } */
|
||||
/* } */
|
||||
|
||||
/* function incrvals($max, $suppress = false) { */
|
||||
/* if ($suppress) */
|
||||
/* //return array(0); */
|
||||
/* return array(0, 4, $max-1); */
|
||||
|
||||
/* //return array(0, 1, $max/3, (int)(($max+1)/2)-1, 2*$max/3, 3*$max/4, 4*$max/5, $max-1); */
|
||||
/* return array(0, 1, 3, $max/2+2, (int)(($max+1)/2)+4, 2*$max/3, 3*$max/4, 4*$max/5, $max-1); */
|
||||
/* } */
|
||||
|
||||
/* echo('<TABLE>' . "\n"); */
|
||||
/* foreach (incrvals(10) AS $y) { */
|
||||
/* foreach (incrvals(12, $y) AS $w) { */
|
||||
/* foreach (incrvals(30, $y||$w) AS $d) { */
|
||||
/* foreach (incrvals(24, $y||$w||$d) AS $h) { */
|
||||
/* foreach (incrvals(60, $y||$w||$d||$h) AS $m) { */
|
||||
/* foreach ($y||$w||$d||$h||$m ? array(0,1,59) : basevals(60) AS $s) { */
|
||||
/* $seconds = 60*(60*(24*(30*(12*$y +$w) + $d) + $h) + $m) + $s; */
|
||||
/* //$seconds = 60*(60*(24*(7*(52*$y +$w) + $d) + $h) + $m) + $s; */
|
||||
/* $age = time() - $seconds; */
|
||||
/* echo ('<TR>' */
|
||||
/* . ' <TD>' . "y:$y; M:$w; d:$d; h:$h; m:$m; s:$s" . '</TD>' */
|
||||
/* . ' <TD>' . FormatHelper::datetime($age) . '</TD>' */
|
||||
/* . ' <TD>' . FormatHelper::age($age) . '</TD>' */
|
||||
/* . ' <TD>' . $seconds . '; days='.($seconds/60/60/24).'</TD>' */
|
||||
/* .'</TR>' . "\n"); */
|
||||
/* } */
|
||||
/* } */
|
||||
/* } */
|
||||
/* } */
|
||||
/* } */
|
||||
/* } */
|
||||
/* echo('</TABLE>' . "\n"); */
|
||||
|
||||
}
|
||||
|
||||
@@ -31,20 +31,14 @@ $rows = array(array('ID', $lease['id']),
|
||||
array('controller' => 'customers',
|
||||
'action' => 'view',
|
||||
$customer['id']))),
|
||||
array('Lease_Date', FormatHelper::date($lease['lease_date'])),
|
||||
array('Move-in Planned', FormatHelper::date($lease['movein_planned_date'])),
|
||||
array('Move-in', (FormatHelper::date($lease['movein_date'])
|
||||
. ' ('
|
||||
. FormatHelper::age($lease['movein_date'])
|
||||
. ')')),
|
||||
array('Move-out', (FormatHelper::date($lease['moveout_date'])
|
||||
. ' ('
|
||||
. FormatHelper::age($lease['moveout_date'])
|
||||
. ')')),
|
||||
array('Move-out Planned', FormatHelper::date($lease['moveout_planned_date'])),
|
||||
array('Notice Given', FormatHelper::date($lease['notice_given_date'])),
|
||||
array('Notice Received', FormatHelper::date($lease['notice_received_date'])),
|
||||
array('Closed', FormatHelper::date($lease['close_date'])),
|
||||
array('Lease_Date', FormatHelper::date($lease['lease_date'], true)),
|
||||
array('Move-in Planned', FormatHelper::date($lease['movein_planned_date'], true)),
|
||||
array('Move-in', FormatHelper::date($lease['movein_date'], true)),
|
||||
array('Move-out', FormatHelper::date($lease['moveout_date'], true)),
|
||||
array('Move-out Planned', FormatHelper::date($lease['moveout_planned_date'], true)),
|
||||
array('Notice Given', FormatHelper::date($lease['notice_given_date'], true)),
|
||||
array('Notice Received', FormatHelper::date($lease['notice_received_date'], true)),
|
||||
array('Closed', FormatHelper::date($lease['close_date'], true)),
|
||||
array('Account', $html->link($account['name'],
|
||||
array('controller' => 'accounts',
|
||||
'action' => 'view',
|
||||
|
||||
Reference in New Issue
Block a user