Completed
Push — master ( dff293...9deb3b )
by Bernhard
10:04
created

Date::roundByInterval()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.2408
c 0
b 0
f 0
cc 5
nc 4
nop 3
1
<?php
2
3
/**
4
 * \Wicked\Timely\Helper\Date
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Bernhard Wick <[email protected]>
15
 * @copyright 2016 Bernhard Wick
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/wick-ed/timely
18
 */
19
20
namespace Wicked\Timely\Helper;
21
22
/**
23
 * Date helper
24
 *
25
 * @author    Bernhard Wick <[email protected]>
26
 * @copyright 2016 Bernhard Wick
27
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
28
 * @link      https://github.com/wick-ed/timely
29
 */
30
class Date
31
{
32
33
    /**
34
     * Constant for the number of seconds in a day
35
     *
36
     * @var integer DAY_IN_SECONDS
37
     */
38
    const DAY_IN_SECONDS = 86400;
39
40
    /**
41
     * Constant for the number of seconds in an hour
42
     *
43
     * @var integer HOUR_IN_SECONDS
44
     */
45
    const HOUR_IN_SECONDS = 3600;
46
47
    /**
48
     * Constant for the number of seconds in a minute
49
     *
50
     * @var integer MINUTE_IN_SECONDS
51
     */
52
    const MINUTE_IN_SECONDS = 60;
53
54
    /**
55
     * Formats a timespan into a string of the format 0d 0h 0m
56
     *
57
     * @param integer $timespan The timespan in seconds to format
58
     *
59
     * @return string
60
     */
61
    public static function secondsToUnits($timespan)
62
    {
63
        // get the days
64
        $days = floor($timespan / self::DAY_IN_SECONDS);
65
        $timespan -= $days * self::DAY_IN_SECONDS;
66
        // get the hours
67
        $hours = floor($timespan / self::HOUR_IN_SECONDS);
68
        $timespan -= $hours * self::HOUR_IN_SECONDS;
69
        // get the minutes
70
        $minutes = round($timespan / self::MINUTE_IN_SECONDS);
71
        // return a formatted string
72
        $result = $days > 0 ? sprintf('%sd ', $days) : '';
73
        $result .= $hours > 0 ? sprintf('%sh ', $hours) : '';
74
        $result .= $minutes > 0 ? sprintf('%sm', $minutes) : '';
75
        return $result;
76
    }
77
78
    /**
79
     * Formats a timespan into a string of the format 0d 0h 0m
80
     *
81
     * @param integer $timespan The timespan in seconds to format
82
     * @param integer $interval The interval in seconds to round against
83
     *
84
     * @return string
85
     */
86
    public static function roundByInterval($timespan, $interval, $enforceMinimum = true)
87
    {
88
        // it could happen that we booked for less than our interval, so it would be rounded down to zero.
89
        // By default we should prevent that
90
        if ($enforceMinimum && $timespan < $interval) {
91
            return $interval;
92
        }
93
94
        // only round if we aren't spot on
95
        $overhead = $timespan % $interval;
96
        if ($overhead === 0) {
97
            return $timespan;
98
        }
99
100
        // round up or down based on our interval
101
        if ($overhead >= ($interval / 2)) {
102
            $timespan += ($interval - $overhead);
103
        } else {
104
            $timespan -= $overhead;
105
        }
106
107
        return $timespan;
108
    }
109
}
110