Skip to content

Commit 6cc4794

Browse files
committed
新增DateTime工具类
1 parent 975fc62 commit 6cc4794

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

src/linktool/DateTime.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
/**
4+
* LinkTool - A useful library for PHP
5+
*
6+
* @author Dong Nan <[email protected]>
7+
* @copyright (c) Dong Nan http://idongnan.cn All rights reserved.
8+
* @link https://github.com/dongnan/LinkTool
9+
* @license BSD (http://opensource.org/licenses/BSD-3-Clause)
10+
*/
11+
12+
namespace linktool;
13+
14+
/**
15+
* DateTime
16+
* 用于处理日期、时间相关的方法
17+
*
18+
* @author Dong Nan <[email protected]>
19+
* @date 2016-5-10
20+
*/
21+
class DateTime {
22+
23+
/**
24+
* 获取标准的日期格式:yyyy-mm-dd
25+
*
26+
* @param string $date
27+
* @return string
28+
*/
29+
public static function getStandardDate($date) {
30+
$year = 0;
31+
$month = 0;
32+
$day = 0;
33+
$match = preg_split('/[\D]0*/', trim($date));
34+
if (empty($match)) {
35+
return '0000-00-00';
36+
}
37+
$dateStr = '';
38+
$matchLen = count($match);
39+
for ($i = 0; $i < $matchLen; $i++) {
40+
$dateStr .= $match[$i] < 10 ? '0' . $match[$i] : $match[$i];
41+
}
42+
$len = strlen($dateStr);
43+
if ($len <= 4) {
44+
$year = intval($dateStr);
45+
} else {
46+
$year = intval(substr($dateStr, 0, 4));
47+
if ($len === 5) {
48+
$month = intval(substr($dateStr, 4, 1));
49+
} else if ($len === 6 || $len === 7) {
50+
if (intval(substr($dateStr, 4, 2)) <= 12) {
51+
$month = intval(substr($dateStr, 4, 2));
52+
$day = intval(substr($dateStr, 6, $len - 6));
53+
} else {
54+
$month = intval(substr($dateStr, 4, 1));
55+
$day = intval(substr($dateStr, 5, $len - 5));
56+
}
57+
} else {
58+
$year = intval(substr($dateStr, 0, 4));
59+
$month = intval(substr($dateStr, 4, 2));
60+
$day = intval(substr($dateStr, 6, 2));
61+
}
62+
}
63+
switch ($month) {
64+
case 1:
65+
case 3:
66+
case 5:
67+
case 7:
68+
case 8:
69+
case 10:
70+
case 12:
71+
$day > 31 && ($day = 0);
72+
break;
73+
case 4:
74+
case 6:
75+
case 9:
76+
case 11:
77+
$day > 30 && ($day = 0);
78+
break;
79+
case 2:
80+
if (($year % 4 === 0 && $year % 100 !== 0) || $year % 400 === 0) {
81+
$day > 29 && ($day = 0);
82+
} else {
83+
$day > 28 && ($day = 0);
84+
}
85+
break;
86+
default:
87+
$month = 0;
88+
$day = 0;
89+
break;
90+
}
91+
92+
return sprintf('%04d-%02d-%02d', $year, $month, $day);
93+
}
94+
95+
/**
96+
* 获取时间戳
97+
*
98+
* @param string $datetime YYYY-MM-DD hh:mm:ss 或 YYYY-MM-DD
99+
* @return int
100+
*/
101+
public static function getUnixTime($datetime) {
102+
if (!preg_match('/[^0-9]/', $datetime)) {
103+
return $datetime;
104+
}
105+
list($y, $m, $d, $h, $i, $s) = explode(' ', preg_replace('/\D+/i', ' ', $datetime));
106+
return mktime($h, $i, $s, $m, $d, $y);
107+
}
108+
109+
/**
110+
* 获取简短的日期显示
111+
*
112+
* @param string $date
113+
* @return string
114+
*/
115+
public static function getShortDate($date) {
116+
return preg_replace('/0000-\d*-\d*|0000-00-\d*|-00-00|-00$/', '', preg_replace('/-|\/|\./', '-', $date));
117+
}
118+
119+
}

0 commit comments

Comments
 (0)