summaryrefslogtreecommitdiff
path: root/app/Helper/Dt.php
diff options
context:
space:
mode:
authorLesstat <florianbarth@gmx.de>2015-07-11 11:44:26 +0200
committerLesstat <florianbarth@gmx.de>2015-07-11 11:44:26 +0200
commita85a1c613239c20fe72eb96c2921f4c220ec156b (patch)
treed032b4591e518cbbbfaa8886f8f5d98a6ea2efb7 /app/Helper/Dt.php
parent5101eaa8060ce3c75a81a26f6e47aae40e3d4ac3 (diff)
parent7e94d0ca233d15d6124c0adf3f956a119c82ccae (diff)
Merged branch 'master' of https://github.com/fguillot/kanboard
only imports conflicted
Diffstat (limited to 'app/Helper/Dt.php')
-rw-r--r--app/Helper/Dt.php114
1 files changed, 114 insertions, 0 deletions
diff --git a/app/Helper/Dt.php b/app/Helper/Dt.php
new file mode 100644
index 00000000..b338fdc8
--- /dev/null
+++ b/app/Helper/Dt.php
@@ -0,0 +1,114 @@
+<?php
+
+namespace Helper;
+
+use DateTime;
+
+/**
+ * DateTime helpers
+ *
+ * @package helper
+ * @author Frederic Guillot
+ */
+class Dt extends \Core\Base
+{
+ /**
+ * Get duration in seconds into human format
+ *
+ * @access public
+ * @param integer $seconds
+ * @return string
+ */
+ public function duration($seconds)
+ {
+ if ($seconds == 0) {
+ return 0;
+ }
+
+ $dtF = new DateTime("@0");
+ $dtT = new DateTime("@$seconds");
+ return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
+ }
+
+ /**
+ * Get the age of an item in quasi human readable format.
+ * It's in this format: <1h , NNh, NNd
+ *
+ * @access public
+ * @param integer $timestamp Unix timestamp of the artifact for which age will be calculated
+ * @param integer $now Compare with this timestamp (Default value is the current unix timestamp)
+ * @return string
+ */
+ public function age($timestamp, $now = null)
+ {
+ if ($now === null) {
+ $now = time();
+ }
+
+ $diff = $now - $timestamp;
+
+ if ($diff < 900) {
+ return t('<15m');
+ }
+ if ($diff < 1200) {
+ return t('<30m');
+ }
+ else if ($diff < 3600) {
+ return t('<1h');
+ }
+ else if ($diff < 86400) {
+ return '~'.t('%dh', $diff / 3600);
+ }
+
+ return t('%dd', ($now - $timestamp) / 86400);
+ }
+
+ /**
+ * Get all hours for day
+ *
+ * @access public
+ * @return array
+ */
+ public function getDayHours()
+ {
+ $values = array();
+
+ foreach (range(0, 23) as $hour) {
+ foreach (array(0, 30) as $minute) {
+ $time = sprintf('%02d:%02d', $hour, $minute);
+ $values[$time] = $time;
+ }
+ }
+
+ return $values;
+ }
+
+ /**
+ * Get all days of a week
+ *
+ * @access public
+ * @return array
+ */
+ public function getWeekDays()
+ {
+ $values = array();
+
+ foreach (range(1, 7) as $day) {
+ $values[$day] = $this->getWeekDay($day);
+ }
+
+ return $values;
+ }
+
+ /**
+ * Get the localized day name from the day number
+ *
+ * @access public
+ * @param integer $day Day number
+ * @return string
+ */
+ public function getWeekDay($day)
+ {
+ return dt('%A', strtotime('next Monday +'.($day - 1).' days'));
+ }
+}