aboutsummaryrefslogtreecommitdiffstats
path: root/include/datetime.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/datetime.php')
-rw-r--r--include/datetime.php37
1 files changed, 37 insertions, 0 deletions
diff --git a/include/datetime.php b/include/datetime.php
index 2182d7c43..89e2876d0 100644
--- a/include/datetime.php
+++ b/include/datetime.php
@@ -268,6 +268,43 @@ function relative_date($posted_date, $format = null) {
return $abs;
}
+/**
+ * @brief Returns a relative time string like 3 seconds ago.
+ * @param string $posted_date (UTC)
+ * @param DateTime $now (optional)
+ * @return string with relative time
+ */
+function relative_time($timestamp, $now = new DateTime()) {
+ $localtime = datetime_convert('UTC', date_default_timezone_get(), $timestamp);
+ $time = new DateTime($localtime);
+
+ $interval = $now->diff($time);
+
+ $prefix = '';
+ $appendix = ' ' . t('ago');
+
+ if ($time > $now) {
+ $prefix = t('in') . ' ';
+ $appendix = '';
+ }
+
+ if ($interval->y > 0) {
+ return $prefix . $interval->y . ' ' . plural_dates('y', $interval->y) . $appendix;
+ } elseif ($interval->m > 0) {
+ return $prefix . $interval->m . ' ' . plural_dates('m', $interval->m) . $appendix;
+ } elseif ($interval->d > 0) {
+ return $prefix . $interval->d . ' ' . plural_dates('d', $interval->d) . $appendix;
+ } elseif ($interval->h > 0) {
+ return $prefix . $interval->h . ' ' . plural_dates('h', $interval->h) . $appendix;
+ } elseif ($interval->i > 0) {
+ return $prefix . $interval->i . ' ' . plural_dates('i', $interval->i) . $appendix;
+ } elseif ($interval->s > 0) {
+ return $prefix . $interval->s . ' ' . plural_dates('s', $interval->s) . $appendix;
+ } else {
+ return t('now');
+ }
+}
+
function plural_dates($k,$n) {
switch($k) {