From: Evan Prodromou Date: Fri, 27 Jun 2008 22:25:22 +0000 (-0400) Subject: more natural date strings X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=aa8dac17d34414b86642715fc1c1e34d8aee2de9;p=quix0rs-gnu-social.git more natural date strings darcs-hash:20080627222522-84dde-9bef8dc543bbb945b1f05dbeed43924d27f704d1.gz --- diff --git a/lib/util.php b/lib/util.php index 4d55e5a69b..c60dc907f0 100644 --- a/lib/util.php +++ b/lib/util.php @@ -726,7 +726,40 @@ function common_path($relative) { function common_date_string($dt) { // XXX: do some sexy date formatting // return date(DATE_RFC822, $dt); - return $dt; + $t = strtotime($dt); + $now = time(); + $diff = $now - $t; + + if ($now < $t) { # that shouldn't happen! + return common_simple_date($dt); + } else if ($diff < 60) { + return _t('a few seconds ago'); + } else if ($diff < 92) { + return _t('about a minute ago'); + } else if ($diff < 3300) { + return _t('about ') . round($diff/60) . _t(' minutes ago'); + } else if ($diff < 5400) { + return _t('about an hour ago'); + } else if ($diff < 22 * 3600) { + return _t('about ') . round($diff/3600) . _t(' hours ago'); + } else if ($diff < 37 * 3600) { + return _t('about a day ago'); + } else if ($diff < 24 * 24 * 3600) { + return _t('about ') . round($diff/(24*3600)) . _t(' days ago'); + } else if ($diff < 46 * 24 * 3600) { + return _t('about a month ago'); + } else if ($diff < 330 * 24 * 3600) { + return _t('about ') . round($diff/(30*24*3600)) . _t(' months ago'); + } else if ($diff < 480 * 24 * 3600) { + return _t('about a year ago'); + } else { + return common_simple_date($dt); + } +} + +function common_simple_date($dt) { + $t = strtotime($dt); + return date(DATE_RFC822, $t); } function common_date_w3dtf($dt) { @@ -1065,6 +1098,8 @@ function common_confirmation_code($bits) { for ($i = 0; $i < $chars; $i++) { # XXX: convert to string and back $num = hexdec(common_good_rand(1)); + # XXX: randomness is too precious to throw away almost + # 40% of the bits we get! $code .= $codechars[$num%32]; } return $code;