summaryrefslogtreecommitdiff
path: root/app/Helper/DateHelper.php
blob: 3844ce64253f8b4b70aabdac330c6c6e9f696846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php

namespace Kanboard\Helper;

use DateTime;
use Kanboard\Core\Base;

/**
 * DateTime helpers
 *
 * @package helper
 * @author  Frederic Guillot
 */
class DateHelper extends Base
{
    /**
     * Get formatted time
     *
     * @access public
     * @param  integer $value
     * @return string
     */
    public function time($value)
    {
        return date($this->config->get('application_time_format', 'H:i'), $value);
    }

    /**
     * Get formatted date
     *
     * @access public
     * @param  integer $value
     * @return string
     */
    public function date($value)
    {
        if (empty($value)) {
            return '';
        }

        if (! ctype_digit($value)) {
            $value = strtotime($value);
        }

        return date($this->config->get('application_date_format', 'm/d/Y'), $value);
    }

    /**
     * Get formatted  datetime
     *
     * @access public
     * @param  integer $value
     * @return string
     */
    public function datetime($value)
    {
        return date($this->config->get('application_datetime_format', 'm/d/Y H:i'), $value);
    }

    /**
     * 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');
        } elseif ($diff < 3600) {
            return t('<1h');
        } elseif ($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 date('l', strtotime('next Monday +'.($day - 1).' days'));
    }
}