1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\DB\SQL; |
4
|
|
|
|
5
|
|
|
use Helix\DB; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Produces datetime related expressions for the instance. |
9
|
|
|
*/ |
10
|
|
|
trait DateTimeTrait { |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var DB |
14
|
|
|
*/ |
15
|
|
|
protected $db; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* `YYYY-MM-DD` |
19
|
|
|
* |
20
|
|
|
* @return Text |
21
|
|
|
*/ |
22
|
|
|
public function getDate () { |
23
|
|
|
return new Text($this->db, $this->getDateTimeFormat('%Y-%m-%d')); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns a text expression using a driver-appropriate format function. |
28
|
|
|
* |
29
|
|
|
* @param string|array $format Format, or formats keyed by driver name. |
30
|
|
|
* @return Text |
31
|
|
|
*/ |
32
|
|
|
public function getDateTimeFormat ($format) { |
33
|
|
|
if (is_array($format)) { |
34
|
|
|
$format = $format[$this->db->getDriver()]; |
35
|
|
|
} |
36
|
|
|
$format = $this->db->quote($format); |
37
|
|
|
switch ($this->db->getDriver()) { |
38
|
|
|
case 'sqlite': |
39
|
|
|
return new Text($this->db, "STRFTIME({$format},{$this})"); |
40
|
|
|
case 'mysql': |
41
|
|
|
default: |
42
|
|
|
return new Text($this->db, "DATE_FORMAT({$this},{$format})"); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* `01` to `31` |
48
|
|
|
* |
49
|
|
|
* @return Numeric |
50
|
|
|
*/ |
51
|
|
|
public function getDay () { |
52
|
|
|
return new Numeric($this->db, $this->getDateTimeFormat('%d')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* `0` to `6` (Sunday is `0`) |
57
|
|
|
* |
58
|
|
|
* @return Numeric |
59
|
|
|
*/ |
60
|
|
|
public function getDayOfWeek () { |
61
|
|
|
return new Numeric($this->db, $this->getDateTimeFormat('%w')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* `001` to `366` |
66
|
|
|
* |
67
|
|
|
* @return Numeric |
68
|
|
|
*/ |
69
|
|
|
public function getDayOfYear () { |
70
|
|
|
return new Numeric($this->db, $this->getDateTimeFormat('%j')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* `00` to `23` |
75
|
|
|
* |
76
|
|
|
* @return Numeric |
77
|
|
|
*/ |
78
|
|
|
public function getHours () { |
79
|
|
|
return new Numeric($this->db, $this->getDateTimeFormat('%H')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* `00` to `59` |
84
|
|
|
* |
85
|
|
|
* @return Numeric |
86
|
|
|
*/ |
87
|
|
|
public function getMinutes () { |
88
|
|
|
return new Numeric($this->db, $this->getDateTimeFormat([ |
89
|
|
|
'mysql' => '%i', |
90
|
|
|
'sqlite' => '%M' |
91
|
|
|
])); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* `01` to `12` |
96
|
|
|
* |
97
|
|
|
* @return Numeric |
98
|
|
|
*/ |
99
|
|
|
public function getMonth () { |
100
|
|
|
return new Numeric($this->db, $this->getDateTimeFormat('%m')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* `00` to `59` |
105
|
|
|
* |
106
|
|
|
* @return Numeric |
107
|
|
|
*/ |
108
|
|
|
public function getSeconds () { |
109
|
|
|
return new Numeric($this->db, $this->getDateTimeFormat('%S')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* `00:00:00` to `23:59:59` |
114
|
|
|
* |
115
|
|
|
* @return Text |
116
|
|
|
*/ |
117
|
|
|
public function getTime () { |
118
|
|
|
return new Text($this->db, $this->getDateTimeFormat([ |
119
|
|
|
'mysql' => '%H:%i:%S', |
120
|
|
|
'sqlite' => '%H:%M:%S' |
121
|
|
|
])); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Unix timestamp. |
126
|
|
|
* |
127
|
|
|
* @return Numeric |
128
|
|
|
*/ |
129
|
|
|
public function getTimestamp () { |
130
|
|
|
switch ($this->db->getDriver()) { |
131
|
|
|
case 'sqlite': |
132
|
|
|
return new Numeric($this->db, "STRFTIME('%s',{$this})"); |
133
|
|
|
default: |
134
|
|
|
return new Numeric($this->db, "UNIX_TIMESTAMP({$this})"); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* `00` to `53` (Sunday is`0`) |
140
|
|
|
* |
141
|
|
|
* @return Numeric |
142
|
|
|
*/ |
143
|
|
|
public function getWeekOfYear () { |
144
|
|
|
return new Numeric($this->db, $this->getDateTimeFormat([ |
145
|
|
|
'mysql' => '%U', |
146
|
|
|
'sqlite' => '%W' |
147
|
|
|
])); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* `YYYY` |
152
|
|
|
* |
153
|
|
|
* @return Numeric |
154
|
|
|
*/ |
155
|
|
|
public function getYear () { |
156
|
|
|
return new Numeric($this->db, $this->getDateTimeFormat('%Y')); |
157
|
|
|
} |
158
|
|
|
} |