Completed
Push — master ( e5ab12...9197f0 )
by Konstantinos
09:09
created

TimeDate::toMysql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 8
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * A class representing a timestamp
5
 */
6
class TimeDate extends Carbon\Carbon
7
{
8
    const DATE_SHORT = "Y-m-d";
9
    const DATE_MEDIUM = "F d";
10
    const DATE_FULL = "F d, Y";
11
    const TIMESTAMP = "H:i";
12
    const TIMESTAMP_FULL = "H:i:s";
13
    const FULL = "Y-m-d H:i:s";
14
    const MYSQL = "Y-m-d H:i:s";
15
16
    /**
17
     * Get the time difference in a human readable format.
18
     *
19
     * @param \Carbon\Carbon|\TimeDate $other
20
     * @param bool                     $absolute Removes time difference modifiers ago, after, etc
21
     *
22
     * @return string The time as a human readable string
23
     */
24 2
    public function diffForHumans(Carbon\Carbon $other = null, $absolute = false)
25
    {
26 2
        if (self::diffInSeconds($other, true) < 4) {
27 2
            return "now";
28
        }
29
        return parent::diffForHumans($other, $absolute);
30
    }
31
32
    /**
33
     * Format the timestamp so that it can be used in mysql queries
34
     * @return string The formatted time
35
     */
36 39
    public function toMysql()
37
    {
38
        // We can't tell MySQL to store timezone data, so just convert the
39
        // timestamp to UTC
40 39
        $time = $this->copy()->setTimezone("UTC");
41
42 39
        return $time->format(self::MYSQL);
43
    }
44
45
    /**
46
     * Create a timestamp from a MySQL string
47
     * @param  string $time
48
     * @return TimeDate
49
     */
50 39
    public static function fromMysql($time)
51
    {
52 39
        return new self($time, "UTC");
53
    }
54
55
    /**
56
     * Create a timestamp from a string or another object
57
     * @param  string|DateTime $time
58
     * @return TimeDate
59
     */
60 39
    public static function from($time)
61
    {
62 39
        if ($time instanceof DateTime) {
63 1
            return self::instance($time);
64
        }
65
66 39
        return new self($time);
67
    }
68
69
    /**
70
     * Get a Carbon instance for the current date and time
71
     *
72
     * @param  DateTimeZone|string $timezone
73
     * @return TimeDate
74
     */
75 3
    public static function now($timezone = null)
76
    {
77 3
        return parent::now($timezone);
78
    }
79
}
80