summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2013-10-04 05:53:13 -0700
committerVincent Petry <pvince81@owncloud.com>2013-10-04 05:53:13 -0700
commit48a4c67d483aee9e9a9e4b883ca085f7aa56e785 (patch)
treea09809b9610d77c4c46065dab571b8a8123aa0b5 /lib
parentb8f2adc7c179bfb8959820bb7e0267ebb56812ed (diff)
parentf3594904c2d2b4a1296ee7dcc65a0c6cefa9ab40 (diff)
downloadnextcloud-server-48a4c67d483aee9e9a9e4b883ca085f7aa56e785.tar.gz
nextcloud-server-48a4c67d483aee9e9a9e4b883ca085f7aa56e785.zip
Merge pull request #5087 from owncloud/templatefunction-relativedate
Added dateOnly argument to relative_modified_date
Diffstat (limited to 'lib')
-rw-r--r--lib/private/template/functions.php45
-rw-r--r--lib/public/template.php4
2 files changed, 39 insertions, 10 deletions
diff --git a/lib/private/template/functions.php b/lib/private/template/functions.php
index 501f8081bff..0aa2b27b96b 100644
--- a/lib/private/template/functions.php
+++ b/lib/private/template/functions.php
@@ -85,22 +85,51 @@ function human_file_size( $bytes ) {
return OC_Helper::humanFileSize( $bytes );
}
-function relative_modified_date($timestamp) {
+/**
+ * @brief Strips the timestamp of its time value
+ * @param int $timestamp UNIX timestamp to strip
+ * @return $timestamp without time value
+ */
+function strip_time($timestamp){
+ $date = new \DateTime("@{$timestamp}");
+ $date->setTime(0, 0, 0);
+ return intval($date->format('U'));
+}
+
+/**
+ * @brief Formats timestamp relatively to the current time using
+ * a human-friendly format like "x minutes ago" or "yesterday"
+ * @param int $timestamp timestamp to format
+ * @param int $fromTime timestamp to compare from, defaults to current time
+ * @param bool $dateOnly whether to strip time information
+ * @return formatted timestamp
+ */
+function relative_modified_date($timestamp, $fromTime = null, $dateOnly = false) {
$l=OC_L10N::get('lib');
- $timediff = time() - $timestamp;
+ if (!isset($fromTime) || $fromTime === null){
+ $fromTime = time();
+ }
+ if ($dateOnly){
+ $fromTime = strip_time($fromTime);
+ $timestamp = strip_time($timestamp);
+ }
+ $timediff = $fromTime - $timestamp;
$diffminutes = round($timediff/60);
$diffhours = round($diffminutes/60);
$diffdays = round($diffhours/24);
$diffmonths = round($diffdays/31);
- if($timediff < 60) { return $l->t('seconds ago'); }
- else if($timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); }
- else if($timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); }
- else if((date('G')-$diffhours) > 0) { return $l->t('today'); }
- else if((date('G')-$diffhours) > -24) { return $l->t('yesterday'); }
+ if(!$dateOnly && $timediff < 60) { return $l->t('seconds ago'); }
+ else if(!$dateOnly && $timediff < 3600) { return $l->n('%n minute ago', '%n minutes ago', $diffminutes); }
+ else if(!$dateOnly && $timediff < 86400) { return $l->n('%n hour ago', '%n hours ago', $diffhours); }
+ else if((date('G', $fromTime)-$diffhours) >= 0) { return $l->t('today'); }
+ else if((date('G', $fromTime)-$diffhours) >= -24) { return $l->t('yesterday'); }
+ // 86400 * 31 days = 2678400
else if($timediff < 2678400) { return $l->n('%n day go', '%n days ago', $diffdays); }
+ // 86400 * 60 days = 518400
else if($timediff < 5184000) { return $l->t('last month'); }
- else if((date('n')-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); }
+ else if((date('n', $fromTime)-$diffmonths) > 0) { return $l->n('%n month ago', '%n months ago', $diffmonths); }
+ // 86400 * 365.25 days * 2 = 63113852
else if($timediff < 63113852) { return $l->t('last year'); }
else { return $l->t('years ago'); }
}
diff --git a/lib/public/template.php b/lib/public/template.php
index 3b1a4ed4906..a5c500b0e25 100644
--- a/lib/public/template.php
+++ b/lib/public/template.php
@@ -90,8 +90,8 @@ function human_file_size( $bytes ) {
* @param $timestamp unix timestamp
* @returns human readable interpretation of the timestamp
*/
-function relative_modified_date($timestamp) {
- return(\relative_modified_date($timestamp));
+function relative_modified_date($timestamp, $dateOnly = false) {
+ return(\relative_modified_date($timestamp, null, $dateOnly));
}