-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDatetime.php
67 lines (55 loc) · 1.72 KB
/
Datetime.php
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
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace axios\tools;
class Datetime
{
private $base_timestamp;
public function __construct($base_timestamp = null)
{
if (null === $base_timestamp) {
$base_timestamp = time();
}
$this->base_timestamp = $base_timestamp;
}
public function hourBeginEnd($hour): array
{
$date = date('Y-m-d', $this->base_timestamp);
$hour = sprintf('%02d', $hour);
$begin = strtotime($date . ' ' . $hour . ':00:00');
$end = strtotime($date . ' ' . $hour . ':00:00 +1 hour -1 seconds');
return [$begin, $end];
}
public function dayBeginEnd($date = null): array
{
if (null === $date) {
$date = date('Y-m-d', $this->base_timestamp);
}
$begin = strtotime($date . ' 00:00:00');
$end = strtotime("{$date} +1 day -1 seconds");
return [$begin, $end];
}
public function monthBeginEnd($year = null, $month = null): array
{
if (null === $year) {
$year = date('Y', $this->base_timestamp);
}
if (null === $month) {
$month = date('m', $this->base_timestamp);
}
$month = sprintf('%02d', $month);
$ymd = $year . '-' . $month . '-01';
$begin = strtotime($ymd . ' 00:00:00');
$end = strtotime("{$ymd} +1 month -1 seconds");
return [$begin, $end];
}
public function yearBeginEnd($year): array
{
if (null === $year) {
$year = date('Y', $this->base_timestamp);
}
$ymd = $year . '-01-01';
$begin = strtotime($ymd . ' 00:00:00');
$end = strtotime("{$ymd} +1 year -1 seconds");
return [$begin, $end];
}
}