Skip to content

Commit 0818499

Browse files
committed
add Datetime
1 parent cef4b29 commit 0818499

File tree

2 files changed

+87
-9
lines changed

2 files changed

+87
-9
lines changed

src/Datetime.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace axios\tools;
6+
7+
class Datetime
8+
{
9+
private $base_timestamp;
10+
11+
public function __construct($base_timestamp = null)
12+
{
13+
if (null === $base_timestamp) {
14+
$base_timestamp = time();
15+
}
16+
$this->base_timestamp = $base_timestamp;
17+
}
18+
19+
public function hourBeginEnd($hour)
20+
{
21+
$date = date('Y-m-d', $this->base_timestamp);
22+
$hour = sprintf('%02d', $hour);
23+
$begin = strtotime($date . ' ' . $hour . ':00:00');
24+
$end = strtotime($date . ' ' . $hour . ':00:00 +1 hour -1 seconds');
25+
26+
return [$begin, $end];
27+
}
28+
29+
public function dayBeginEnd($date = null)
30+
{
31+
if (null === $date) {
32+
$date = date('Y-m-d', $this->base_timestamp);
33+
}
34+
$begin = strtotime($date . ' 00:00:00');
35+
$end = strtotime("{$date} +1 day -1 seconds");
36+
37+
return [$begin, $end];
38+
}
39+
40+
public function monthBeginEnd($year = null, $month = null)
41+
{
42+
if (null === $year) {
43+
$year = date('Y', $this->base_timestamp);
44+
}
45+
if (null === $month) {
46+
$month = date('m', $this->base_timestamp);
47+
}
48+
$month = sprintf('%02d', $month);
49+
$ymd = $year . '-' . $month . '-01';
50+
$begin = strtotime($ymd . ' 00:00:00');
51+
$end = strtotime("{$ymd} +1 month -1 seconds");
52+
53+
return [$begin, $end];
54+
}
55+
}

src/Helper.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@
66

77
class Helper
88
{
9+
/**
10+
* @param $date
11+
* @param $hour
12+
* @param string $format
13+
*
14+
* @return array
15+
*
16+
* @deprecated use Datetime instead, pls
17+
*/
918
public static function getHourBeginEndTime($date, $hour, $format = 'timestamp')
1019
{
11-
$hour = sprintf('%02d', $hour);
12-
$begin = strtotime($date . ' ' . $hour . ':00:00');
13-
$end = strtotime($date . ' ' . $hour . ':00:00 +1 hour -1 seconds');
20+
$datetime = new Datetime(strtotime($date));
21+
list($begin, $end) = $datetime->hourBeginEnd($hour);
1422
if ('timestamp' == $format) {
1523
return [
1624
'begin' => $begin,
@@ -24,10 +32,18 @@ public static function getHourBeginEndTime($date, $hour, $format = 'timestamp')
2432
];
2533
}
2634

35+
/**
36+
* @param $date
37+
* @param string $format
38+
*
39+
* @return array
40+
*
41+
* @deprecated use Datetime instead, pls
42+
*/
2743
public static function getDayBeginEndTime($date, $format = 'timestamp')
2844
{
29-
$begin = strtotime($date . ' 00:00:00');
30-
$end = strtotime("{$date} +1 day -1 seconds");
45+
$datetime = new Datetime();
46+
list($begin, $end) = $datetime->dayBeginEnd($date);
3147
if ('timestamp' == $format) {
3248
return [
3349
'begin' => $begin,
@@ -41,12 +57,19 @@ public static function getDayBeginEndTime($date, $format = 'timestamp')
4157
];
4258
}
4359

60+
/**
61+
* @param $year
62+
* @param $month
63+
* @param string $format
64+
*
65+
* @return array
66+
*
67+
* @deprecated use Datetime instead, pls
68+
*/
4469
public static function getMonthBeginEndDay($year, $month, $format = 'timestamp')
4570
{
46-
$month = sprintf('%02d', $month);
47-
$ymd = $year . '-' . $month . '-01';
48-
$begin = strtotime($ymd . ' 00:00:00');
49-
$end = strtotime("{$ymd} +1 month -1 seconds");
71+
$datetime = new Datetime();
72+
list($begin, $end) = $datetime->monthBeginEnd($year, $month);
5073
if ('timestamp' == $format) {
5174
return [
5275
'begin' => $begin,

0 commit comments

Comments
 (0)