DateTime::mysql_now()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Vectorface\MySQLite\MySQL;
4
5
/**
6
 * Provides Date and Time MySQL compatibility functions for SQLite.
7
 *
8
 * http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
9
 */
10
trait DateTime
11
{
12
    /**
13
     * NOW - Return the current date and time
14
     *
15
     * @return string The current timestamp, in MySQL's date format.
16
     */
17 1
    public static function mysql_now()
18
    {
19 1
        return date("Y-m-d H:i:s");
20
    }
21
22
    /**
23
     * TO_DAYS - Return the date argument converted to days
24
     *
25
     * @param string $date A date to be converted to days.
26
     * @return int The date converted to a number of days since year 0.
27
     */
28 1
    public static function mysql_to_days($date)
29
    {
30
        // Why does this work on my Debian machine with PHP 5.6, and not on Travis?
31
        // - strtotime("0000-12-31") yields -62135665200
32
        // - 60 * 60 * 24 is 86400 (seconds)
33
        // - 1413108827 / 86400 = -719162.79166667, python similarly says -719162.7916666666
34
        // - ceil bumps it up 1 to -719162
35
        // - 719527 + (-719162), python agrees
36
        // - So why is Travis giving me 364?!? Their PHP is configured for a different timezone (I think)!
37 1
        return intval(719528 + ((strtotime($date) - date("Z")) / (60 * 60 * 24)));
38
    }
39
40
    /**
41
     * UNIX_TIMESTAMP - Return a UNIX timestamp
42
     *
43
     * @param string $date The date to be converted to a unix timestamp. Defaults to the current date/time.
44
     * @return int The number of seconds since the unix epoch.
45
     */
46 1
    public static function mysql_unix_timestamp($date = null)
47
    {
48 1
        if (!isset($date)) {
49 1
            return time();
50
        } elseif (count_chars(str_replace([":", "-", " "], "", $date), 3) === "0") {
51
            return 0; /* MySQL's implementation returns zero for 0000-00-00 00:00:00 and similar */
52 1
        }
53
54
        return strtotime($date);
55
    }
56
}
57