-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDateTime.php
More file actions
56 lines (51 loc) · 1.83 KB
/
DateTime.php
File metadata and controls
56 lines (51 loc) · 1.83 KB
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
<?php
namespace Vectorface\MySQLite\MySQL;
/**
* Provides Date and Time MySQL compatibility functions for SQLite.
*
* http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
*/
trait DateTime
{
/**
* NOW - Return the current date and time
*
* @return string The current timestamp, in MySQL's date format.
*/
public static function mysql_now()
{
return date("Y-m-d H:i:s");
}
/**
* TO_DAYS - Return the date argument converted to days
*
* @param string $date A date to be converted to days.
* @return int The date converted to a number of days since year 0.
*/
public static function mysql_to_days($date)
{
// Why does this work on my Debian machine with PHP 5.6, and not on Travis?
// - strtotime("0000-12-31") yields -62135665200
// - 60 * 60 * 24 is 86400 (seconds)
// - 1413108827 / 86400 = -719162.79166667, python similarly says -719162.7916666666
// - ceil bumps it up 1 to -719162
// - 719527 + (-719162), python agrees
// - So why is Travis giving me 364?!? Their PHP is configured for a different timezone (I think)!
return intval(719528 + ((strtotime($date) - date("Z")) / (60 * 60 * 24)));
}
/**
* UNIX_TIMESTAMP - Return a UNIX timestamp
*
* @param string $date The date to be converted to a unix timestamp. Defaults to the current date/time.
* @return int The number of seconds since the unix epoch.
*/
public static function mysql_unix_timestamp($date = null)
{
if (!isset($date)) {
return time();
} elseif (count_chars(str_replace([":", "-", " "], "", $date), 3) === "0") {
return 0; /* MySQL's implementation returns zero for 0000-00-00 00:00:00 and similar */
}
return strtotime($date);
}
}