1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helix\DB\Fluent\DateTime; |
4
|
|
|
|
5
|
|
|
use Helix\DB\Fluent\DateTime; |
6
|
|
|
use Helix\DB\Fluent\Num; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Date-time diffing. |
10
|
|
|
*/ |
11
|
|
|
trait DateTimeDiffTrait |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
use DateTimeFormatTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Date-time difference (`$x - $this`) in fractional days elapsed. |
18
|
|
|
* |
19
|
|
|
* @param null|DateTime $x Defaults to the current time. |
20
|
|
|
* @return Num |
21
|
|
|
*/ |
22
|
|
|
public function diffDays(DateTime $x = null) |
23
|
|
|
{ |
24
|
|
|
return ($x ?? DateTime::now($this->db))->julian()->sub($this->julian()); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Date-time difference (`$x - $this`) in fractional hours elapsed. |
29
|
|
|
* |
30
|
|
|
* @param null|DateTime $x Defaults to the current time. |
31
|
|
|
* @return Num |
32
|
|
|
*/ |
33
|
|
|
public function diffHours(DateTime $x = null) |
34
|
|
|
{ |
35
|
|
|
return $this->diffDays($x)->mul(24); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Date-time difference (`$x - $this`) in fractional minutes elapsed. |
40
|
|
|
* |
41
|
|
|
* @param null|DateTime $x Defaults to the current time. |
42
|
|
|
* @return Num |
43
|
|
|
*/ |
44
|
|
|
public function diffMinutes(DateTime $x = null) |
45
|
|
|
{ |
46
|
|
|
return $this->diffDays($x)->mul(24 * 60); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Date-time difference (`$x - $this`) in fractional months elapsed. |
51
|
|
|
* |
52
|
|
|
* @param null|DateTime $x Defaults to the current time. |
53
|
|
|
* @return Num |
54
|
|
|
*/ |
55
|
|
|
public function diffMonths(DateTime $x = null) |
56
|
|
|
{ |
57
|
|
|
return $this->diffDays($x)->div(365.2425 / 12); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Date-time difference (`$x - $this`) in fractional seconds elapsed. |
62
|
|
|
* |
63
|
|
|
* @param null|DateTime $x Defaults to the current time. |
64
|
|
|
* @return Num |
65
|
|
|
*/ |
66
|
|
|
public function diffSeconds(DateTime $x = null) |
67
|
|
|
{ |
68
|
|
|
return $this->diffDays($x)->mul(24 * 60 * 60); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Date-time difference (`$x - $this`) in fractional years elapsed. |
73
|
|
|
* |
74
|
|
|
* @param null|DateTime $x Defaults to the current time. |
75
|
|
|
* @return Num |
76
|
|
|
*/ |
77
|
|
|
public function diffYears(DateTime $x = null) |
78
|
|
|
{ |
79
|
|
|
return $this->diffDays($x)->div(365.2425); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|