Skip to content

Commit d0b3098

Browse files
committed
new: add some new tool class files
1 parent b4e1eab commit d0b3098

File tree

12 files changed

+798
-23
lines changed

12 files changed

+798
-23
lines changed

src/Helper/DateHelper.php

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/stdlib.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/stdlib
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\Stdlib\Helper;
11+
12+
use function date;
13+
use function floor;
14+
use function is_numeric;
15+
use function strlen;
16+
use function strtotime;
17+
18+
/**
19+
* Class DateHelper
20+
*/
21+
class DateHelper
22+
{
23+
/**
24+
* 判断给定的 字符串 是否是个 时间戳
25+
*
26+
* @param int|string $timestamp 时间戳
27+
*
28+
* @return bool
29+
*/
30+
public static function isTimestamp($timestamp): bool
31+
{
32+
if (!is_numeric($timestamp) || 10 !== strlen($timestamp)) {
33+
return false;
34+
}
35+
36+
return (bool)date('Ymd', $timestamp);
37+
}
38+
39+
/**
40+
* 校验值是否是日期格式
41+
*
42+
* @param string $date 日期
43+
*
44+
* @return boolean
45+
*/
46+
public static function isDate(string $date): bool
47+
{
48+
return strtotime($date) > 0;
49+
}
50+
51+
/**
52+
* 校验值是否是日期并且是否满足设定格式
53+
*
54+
* @param string $date 日期
55+
* @param string $format 需要检验的格式数组
56+
*
57+
* @return boolean
58+
*/
59+
public static function isDateFormat(string $date, string $format = 'Y-m-d'): bool
60+
{
61+
if (!$unixTime = strtotime($date)) {
62+
return false;
63+
}
64+
65+
// 校验日期的格式有效性
66+
if (date($format, $unixTime) === $date) {
67+
return true;
68+
}
69+
70+
return false;
71+
}
72+
73+
/**
74+
* @return int
75+
*/
76+
public static function todayStart(): int
77+
{
78+
return strtotime('today 00:00:00');
79+
}
80+
81+
/**
82+
* @return int
83+
*/
84+
public static function todayEnd(): int
85+
{
86+
return strtotime('today 23:59:59');
87+
}
88+
89+
/**
90+
* @return false|int
91+
*/
92+
public static function tomorrowBegin()
93+
{
94+
return mktime(0, 0, 0, date('m'), date('d') + 1, date('Y'));
95+
}
96+
97+
/**
98+
* @return int
99+
*/
100+
public static function tomorrowStart(): int
101+
{
102+
return strtotime('+1 day 00:00:00');
103+
}
104+
105+
/**
106+
* @return int
107+
*/
108+
public static function tomorrowEnd(): int
109+
{
110+
return strtotime('+1 day 23:59:59');
111+
}
112+
113+
/**
114+
* @return int
115+
*/
116+
public static function tomorrow(): int
117+
{
118+
return strtotime('+1 day');
119+
}
120+
121+
/**
122+
* 获取指定日期所在月的第一天和最后一天
123+
*
124+
* @param string $date
125+
*
126+
* @return array
127+
*/
128+
public static function getTheMonth(string $date): array
129+
{
130+
$firstDay = date('Y-m-01', strtotime($date));
131+
$lastDay = date('Y-m-d', strtotime("$firstDay +1 month -1 day"));
132+
133+
return [$firstDay, $lastDay];
134+
}
135+
136+
/**
137+
* 获取指定日期上个月的第一天和最后一天
138+
*
139+
* @param string $date
140+
*
141+
* @return array
142+
*/
143+
public static function getPurMonth(string $date): array
144+
{
145+
$time = strtotime($date);
146+
147+
$firstDay = date('Y-m-01', strtotime(date('Y', $time) . '-' . (date('m', $time) - 1) . '-01'));
148+
$lastDay = date('Y-m-d', strtotime("$firstDay +1 month -1 day"));
149+
150+
return [$firstDay, $lastDay];
151+
}
152+
153+
/**
154+
* 获取指定日期下个月的第一天和最后一天
155+
*
156+
* @param string $date
157+
*
158+
* @return array
159+
*/
160+
public static function getNextMonth(string $date): array
161+
{
162+
$arr = getdate();
163+
164+
if ($arr['mon'] === 12) {
165+
$year = $arr['year'] + 1;
166+
$month = $arr['mon'] - 11;
167+
$day = $arr['mday'];
168+
169+
$mday = $day < 10 ? '0' . $day : $day;
170+
171+
$firstDay = $year . '-0' . $month . '-01';
172+
$lastDay = $year . '-0' . $month . '-' . $mday;
173+
} else {
174+
$time = strtotime($date);
175+
$firstDay = date('Y-m-01', strtotime(date('Y', $time) . '-' . (date('m', $time) + 1) . '-01'));
176+
$lastDay = date('Y-m-d', strtotime("$firstDay +1 month -1 day"));
177+
}
178+
179+
return [$firstDay, $lastDay];
180+
}
181+
182+
/**
183+
* 获得几天前,几小时前,几月前
184+
*
185+
* @param int $time
186+
* @param array $unit
187+
*
188+
* @return string
189+
*/
190+
public static function before(int $time, array $unit = []): ?string
191+
{
192+
$unit = $unit ?: ['', '', '星期', '', '小时', '分钟', ''];
193+
194+
$nowTime = time();
195+
$diffTime = $nowTime - $time;
196+
197+
switch (true) {
198+
case $time < ($nowTime - 31536000):
199+
return floor($diffTime / 31536000) . $unit[0];
200+
case $time < ($nowTime - 2592000):
201+
return floor($diffTime / 2592000) . $unit[1];
202+
case $time < ($nowTime - 604800):
203+
return floor($diffTime / 604800) . $unit[2];
204+
case $time < ($nowTime - 86400):
205+
return floor($diffTime / 86400) . $unit[3];
206+
case $time < ($nowTime - 3600):
207+
return floor($diffTime / 3600) . $unit[4];
208+
case $time < ($nowTime - 60):
209+
return floor($diffTime / 60) . $unit[5];
210+
default:
211+
return floor($diffTime) . $unit[6];
212+
}
213+
}
214+
}

0 commit comments

Comments
 (0)