1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Daaner\NovaPoshta\Traits; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Support\Facades\Date; |
8
|
|
|
|
9
|
|
|
trait DateTimes |
10
|
|
|
{ |
11
|
|
|
protected $dateTime; |
12
|
|
|
protected $dateTimeFrom; |
13
|
|
|
protected $dateTimeTo; |
14
|
|
|
protected $format = 'd.m.Y'; |
15
|
|
|
protected $formatTime = 'd.m.Y H:i:s'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param string|Carbon|date $dateTime |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function setDateTime($dateTime): void |
22
|
|
|
{ |
23
|
|
|
$this->dateTime = $this->checkDate($dateTime); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string|Carbon|date $dateTimeFrom |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function setDateTimeFrom($dateTimeFrom): void |
31
|
|
|
{ |
32
|
|
|
$this->dateTimeFrom = $this->checkDate($dateTimeFrom); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string|Carbon|date $dateTimeTo |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
public function setDateTimeTo($dateTimeTo): void |
40
|
|
|
{ |
41
|
|
|
$this->dateTimeTo = $this->checkDate($dateTimeTo); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function getDateTime(): void |
48
|
|
|
{ |
49
|
|
|
if ($this->dateTime && (! $this->dateTimeFrom || ! $this->dateTimeTo)) { |
50
|
|
|
$this->methodProperties['DateTime'] = $this->dateTime; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (! $this->dateTime) { |
54
|
|
|
$this->dateTime = Carbon::now()->format($this->format); |
55
|
|
|
$this->methodProperties['DateTime'] = $this->dateTime; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function getDateTimeFromTo(): void |
63
|
|
|
{ |
64
|
|
|
if ($this->dateTimeFrom || $this->dateTimeTo) { |
65
|
|
|
if (! $this->dateTimeTo) { |
66
|
|
|
$this->dateTimeTo = Carbon::now()->format($this->format); |
67
|
|
|
} |
68
|
|
|
if (! $this->dateTimeFrom) { |
69
|
|
|
$this->dateTimeFrom = $this->dateTimeTo; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->methodProperties['DateTimeFrom'] = $this->dateTimeFrom; |
73
|
|
|
$this->methodProperties['DateTimeTo'] = $this->dateTimeTo; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Странно, но тут с минутами и секундами. |
79
|
|
|
* |
80
|
|
|
* @param string|Carbon|date|null $from |
81
|
|
|
* @param string|Carbon|date|null $to |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
public function getDateFromTo($from = null, $to = null): void |
85
|
|
|
{ |
86
|
|
|
// DateFrom |
87
|
|
|
if ($from) { |
88
|
|
|
$from = $this->checkDate($from, $this->formatTime); |
|
|
|
|
89
|
|
|
} else { |
90
|
|
|
$from = Carbon::now()->addMonth(-3)->format($this->formatTime); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// DateTo |
94
|
|
|
if ($to) { |
95
|
|
|
$to = $this->checkDate($to, $this->formatTime); |
96
|
|
|
} else { |
97
|
|
|
$to = Carbon::now()->format($this->formatTime); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->methodProperties['DateFrom'] = $from; |
101
|
|
|
$this->methodProperties['DateTo'] = $to; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string|Carbon $date |
106
|
|
|
* @return string $date |
107
|
|
|
*/ |
108
|
|
|
public function checkDate($date, ?string $format = null): string |
109
|
|
|
{ |
110
|
|
|
if(! $format) { |
111
|
|
|
$format = $this->format; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($date instanceof Carbon) { |
115
|
|
|
$date = $date->format($format); |
116
|
|
|
} else { |
117
|
|
|
try { |
118
|
|
|
$date = Carbon::parse($date)->format($format); |
119
|
|
|
} catch (Exception $e) { |
120
|
|
|
$date = Carbon::now()->format($format); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $date; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|