Completed
Push — master ( 7f4fe5...70b2c3 )
by Peter
02:55
created

Util::roundDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Bundle\DateBundle;
11
12
class Util
13
{
14
    /**
15
     * Bugfix for get monday of this week.
16
     *
17
     * @see https://bugs.php.net/bug.php?id=63740
18
     * @codeCoverageIgnore
19
     *
20
     * @param \DateTime $date
21
     *
22
     * @return \DateTime
23
     */
24
    public static function getMondayThisWeek(\DateTime $date)
25
    {
26
        $monday = clone $date;
27
28
        if ($monday->format('N') != 1) {
29
            $monday->modify('Monday this week');
30
31
            if ($date->format('Y W') != $monday->format('Y W')) {
32
                $monday->modify('-7 day');
33
            }
34
        }
35
36
        return $monday;
37
    }
38
39
    /**
40
     * @param \DateTime $date
41
     * @param int $seconds
42
     *
43
     * @return \DateTime
44
     */
45 8
    public static function roundDate(\DateTime $date, $seconds)
46
    {
47 8
        $date = clone $date;
48
49 8
        return $date->setTimestamp(round($date->getTimestamp() / $seconds) * $seconds);
50
    }
51
}
52