Date.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. <?php
  2. namespace common\helpers;
  3. use yii\db\Expression;
  4. /**
  5. * 日期
  6. *
  7. * @package Helper
  8. * @author Aries <forphp@qq.com>
  9. */
  10. class Date {
  11. const OCI_TIME_FORMAT_FULL = 'YYYY-MM-DD HH24:MI:SS';
  12. const OCI_TIME_FORMAT_DATE = 'YYYY-MM-DD';
  13. const OCI_TIME_FORMAT_MONTH = 'YYYY-MM';
  14. const OCI_TIME_FORMAT_YEAR = 'YYYY';
  15. const OCI_TIME_FORMAT_SHORT_DATE = 'YYYYMMDD';
  16. const OCI_TIME_FORMAT_SHORT_MONTH = 'YYYYMM';
  17. const OCI_DATE_FORMAT_PHP = 'Y-m-d';
  18. /**
  19. * 检测日期的格式
  20. * @param $value
  21. * @return bool
  22. */
  23. public static function validator($value) {
  24. if (!preg_match("#^\d{4}([/-])([0][0-9]|[1][0-2])\\1([0-2][0-9]|[3][0-1])$#", $value)) {
  25. return false;
  26. }
  27. return true;
  28. }
  29. /**
  30. * @param int $days
  31. * @return int|mixed
  32. */
  33. public static function nowTime($days = 0) {
  34. $days = \Yii::$app->params['daysDiff'];
  35. if (\Yii::$app->id == 'app-console') {
  36. return time() + $days * 24 * 60 * 60;
  37. } else {
  38. return __SYSTEM_TIME__ + $days * 24 * 60 * 60;
  39. }
  40. }
  41. /**
  42. * 时间转换
  43. *
  44. * @param null $value
  45. * @param string $format
  46. * @return false|null|string
  47. */
  48. public static function convert($value = null, $format = 'Y-m-d') {
  49. if (is_null($value)) $value = date($format, self::nowTime()); //相对于某一时间的昨天
  50. if (Validator::validateQuickLy('chMonth', $value)) {
  51. return $value . '-01';
  52. }
  53. if (!Validator::validateQuickLy('chDate', $value)) {
  54. $value = date($format, $value);
  55. }
  56. return $value;
  57. }
  58. /**
  59. * @param string $format
  60. * @return false|string
  61. */
  62. public static function today($format = 'Y-m-d') {
  63. return date($format, self::nowTime());
  64. }
  65. /**
  66. * @return false|int
  67. */
  68. public static function todayStart() {
  69. $relative = self::convert();
  70. return strtotime($relative);
  71. }
  72. /**
  73. * @return false|int
  74. */
  75. public static function todayEnd() {
  76. $relative = self::convert();
  77. return strtotime($relative . ' +1 day') - 1;
  78. }
  79. /**
  80. * @param null $relative
  81. * @param string $format
  82. * @return false|int|string
  83. */
  84. public static function yesterday($relative = null, $format = null) {
  85. $relative = self::convert($relative);
  86. $yestoday = strtotime($relative . '-1 day');
  87. if (!is_null($format)) {
  88. return date($format, $yestoday);
  89. }
  90. return $yestoday;
  91. }
  92. /**
  93. * 昨天的开始时间[0点]
  94. *
  95. * @param null $relative
  96. * @return false|int
  97. */
  98. public static function yesterdayStart($relative = null) {
  99. $date = self::yesterday($relative, 'Y-m-d');
  100. return strtotime($date);
  101. }
  102. /**
  103. * @param null $relative
  104. * @return false|int
  105. */
  106. public static function yesterdayEnd($relative = null) {
  107. $date = self::yesterday($relative, 'Y-m-d');
  108. return strtotime($date . ' +1 day') - 1;
  109. }
  110. /**
  111. * @param null $relative
  112. * @param string $format
  113. * @return false|int|string
  114. */
  115. public static function lastMonth($relative = null, $format = null) {
  116. $relative = self::convert($relative);
  117. $lastMonth = strtotime($relative . 'first day of previous month');
  118. if(!is_null($format)){
  119. return date($format ,$lastMonth);
  120. }
  121. return $lastMonth;
  122. }
  123. /**
  124. * @param int $num
  125. * @param null $relative
  126. * @param null $format
  127. * @return false|int|string
  128. */
  129. public static function lastNumMonth($num = 1, $relative = null, $format = null) {
  130. $relative = self::convert($relative,'Y-m');
  131. $lastMonth = strtotime($relative . '-' . $num . ' month');
  132. if (!is_null($format)) {
  133. return date($format, $lastMonth);
  134. }
  135. return $lastMonth;
  136. }
  137. /**
  138. * 上几天的时间
  139. * @param int $num
  140. * @param null $relative
  141. * @param null $format
  142. * @return false|int|string
  143. */
  144. public static function lastNumDay($num = 1, $relative = null, $format = null) {
  145. $relative = self::convert($relative,'Y-m-d');
  146. $lastDay = strtotime($relative . '-' . $num . ' day');
  147. if (!is_null($format)) {
  148. return date($format, $lastDay);
  149. }
  150. return $lastDay;
  151. }
  152. /**
  153. * 下月
  154. * @param null $relative
  155. * @param null $format
  156. * @return false|int|string
  157. */
  158. public static function nextMonth($relative = null, $format = null) {
  159. $relative = self::convert($relative);
  160. $nextMonth = strtotime($relative . ' first day of next month');
  161. if(!is_null($format)){
  162. return date($format ,$nextMonth);
  163. }
  164. return $nextMonth;
  165. }
  166. /**
  167. * 下个月的指定日期
  168. * @param null $relative
  169. * @param null $format
  170. * @return false|int
  171. */
  172. public static function nextMonthDay($relative = null, $format = null) {
  173. $day = date('d', $relative);
  174. $nextMonthFirstDay = self::nextMonth($relative, $format = null);
  175. return strtotime(date('Y-m-'.$day, $nextMonthFirstDay));
  176. }
  177. /**
  178. * 一年的开始
  179. * @param null $relative
  180. * @return false|int
  181. */
  182. public static function yearStart($relative = null) {
  183. $relative = self::convert($relative, 'Y-01-01');
  184. return strtotime($relative);
  185. }
  186. /**
  187. * 一年的结束
  188. * @param null $relative
  189. * @return false|int
  190. */
  191. public static function yearEnd($relative = null) {
  192. $relative = self::convert($relative, 'Y-12-31');
  193. return strtotime($relative . ' +1 day') - 1;
  194. }
  195. /**
  196. * 月开始时间
  197. *
  198. * @param null $relative
  199. * @return false|int
  200. */
  201. public static function monthStart($relative = null) {
  202. $relative = self::convert($relative, 'Y-m-01');
  203. return strtotime($relative);
  204. }
  205. /**
  206. * 月结束时间[23:59:59]
  207. *
  208. * @param null $relative
  209. * @return false|int
  210. */
  211. public static function monthEnd($relative = null) {
  212. $firstday = self::convert($relative, 'Y-m-01');
  213. return strtotime($firstday . " +1 month") - 1;
  214. }
  215. /**
  216. * 某一天开始的时间 00:00:00
  217. *
  218. * @param null $relative
  219. * @return false|int
  220. */
  221. public static function dayStart($relative = null) {
  222. $day = self::convert($relative);
  223. return strtotime($day);
  224. }
  225. /**
  226. * 某一天开始的时间 23:59:59
  227. *
  228. * @param null $relative
  229. * @return false|int
  230. */
  231. public static function dayEnd($relative = null) {
  232. $day = self::convert($relative);
  233. return strtotime($day . ' +1 day') - 1;
  234. }
  235. /**
  236. * 一天中某个小时的时间戳
  237. * @param null $relative
  238. * @param $hour
  239. * @return false|int
  240. */
  241. public static function dayHour($relative = null, $hour = '00') {
  242. $day = self::convert($relative);
  243. $day = $day . " $hour:00:00";
  244. return strtotime($day);
  245. }
  246. /**
  247. * 一天中某小时某分钟的时间戳
  248. * @param null $relative
  249. * @param string $hourMinute
  250. * @return false|int
  251. */
  252. public static function dayMinute($relative = null, $hourMinute = '00:00') {
  253. $day = self::convert($relative);
  254. $day = $day . " $hourMinute:00";
  255. return strtotime($day);
  256. }
  257. /**
  258. * 本周的开始到结束
  259. *
  260. * @param null $relative
  261. * @return array
  262. */
  263. public static function thisWeek($relative = null) {
  264. if (is_null($relative)) {
  265. $relative = self::nowTime();
  266. } else if (Validator::validateQuickLy('chDate', $relative)) {
  267. $relative = strtotime($relative);
  268. }
  269. $startTime = strtotime('last Monday', $relative); //周一
  270. $endTime = strtotime('Sunday', $relative); //周日
  271. return [
  272. 'start' => self::dayStart($startTime),
  273. 'end' => self::dayEnd($endTime),
  274. ];
  275. }
  276. /**
  277. * 七天的数据
  278. *
  279. * @param string $format
  280. * @param null $relative
  281. * @return array
  282. */
  283. public static function sevenDays($format = 'Y-m-d', $relative = null) {
  284. $data = [];
  285. if (is_null($relative)) $relative = self::nowTime();
  286. if (Validator::validateQuickLy('chDate', $relative)) {
  287. $relative = strtotime($relative);
  288. }
  289. for ($i = 7; $i >= 0; $i--) {
  290. $time = strtotime("-$i day", $relative);
  291. $data[] = [
  292. 'date' => date($format, $time),
  293. 'time' => $time,
  294. 'shortDate' => date('m-d', $time),
  295. ];
  296. }
  297. $data[] = [
  298. 'date' => date($format, $relative),
  299. 'time' => $relative,
  300. 'shortDate' => date('m-d', $relative),
  301. ];
  302. return $data;
  303. }
  304. /**
  305. * 半年
  306. *
  307. * @param string $format
  308. * @param null $relative
  309. * @return array
  310. */
  311. public static function halfYear($format = 'Y-m-d', $relative = null) {
  312. $data = [];
  313. if (is_null($relative)) $relative = self::nowTime();
  314. if (Validator::validateQuickLy('chDate', $relative)) {
  315. $relative = strtotime($relative);
  316. }
  317. for ($i = 5; $i >= 0; $i--) {
  318. $time = strtotime("-$i month", $relative);
  319. $month = date('n', $time);
  320. $data[] = [
  321. 'date' => date($format, $time),
  322. 'time' => $time,
  323. 'month' => $month,
  324. ];
  325. }
  326. return $data;
  327. }
  328. /**
  329. * 日期间的差
  330. * @param $start
  331. * @param $end
  332. * @return \DateInterval|false
  333. */
  334. public static function diff($start, $end) {
  335. $start = Validator::validateQuickLy('chDate', $start) ? $start : date('Y-m-d', $start);
  336. $end = Validator::validateQuickLy('chDate', $end) ? $end : date('Y-m-d', $end);
  337. $sdate = date_create($start);
  338. $edate = date_create($end);
  339. $diff = date_diff($sdate, $edate);
  340. $result = [];
  341. $result['y'] = $diff->y + $diff->m / 12 + $diff->d / 365.25;
  342. $result['m'] = $diff->y * 12 + $diff->m + $diff->d / 30 + $diff->h / 24;
  343. $result['d'] = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h / 24 + $diff->i / 60;
  344. $result['h'] = ($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h + $diff->i / 60;
  345. $result['i'] = (($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i + $diff->s / 60;
  346. $result['s'] = ((($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i) * 60 + $diff->s;
  347. return $result;
  348. }
  349. /**
  350. * 所传日期是周几
  351. * @param $relative
  352. * @return false|string
  353. */
  354. public static function dateWeek($relative = null) {
  355. $day = self::convert($relative);
  356. return date('w', strtotime($day));
  357. }
  358. /**
  359. * 是不是UNIXTIME
  360. * @param $time
  361. * @return bool
  362. */
  363. public static function isUnixtime($time) {
  364. $len = strlen($time);
  365. if ($len < 10) {
  366. return false;
  367. }
  368. if ($len > 10) {
  369. $time = substr($time, 0, 10);
  370. }
  371. return ctype_digit($time) && $time <= 2147483647;
  372. }
  373. /**
  374. * 获取Oracle支持的to_date的时间格式
  375. * @param null $dateTime
  376. * @param string $format
  377. * @return Expression
  378. */
  379. public static function ociToDate($dateTime = null, $format = self::OCI_TIME_FORMAT_FULL) {
  380. if ($dateTime === null) {
  381. $dateTime = self::nowTime();
  382. }
  383. // if (self::isUnixtime($dateTime)) {
  384. // $dateTime = date('Y-m-d H:i:s', $dateTime);
  385. // }
  386. //
  387. // return new Expression("TO_DATE('$dateTime', '$format')");
  388. if (!self::isUnixtime($dateTime)) {
  389. $dateTime = strtotime($dateTime);
  390. }
  391. return date('Y-m-d', $dateTime);
  392. }
  393. /**
  394. * UTC时间转时间戳
  395. * @param $dateTime
  396. * @return false|int
  397. */
  398. public static function utcToTime($dateTime) {
  399. return strtotime($dateTime);
  400. }
  401. /**
  402. * 是否是正确的年月格式
  403. * @param $yearMonth
  404. * @return false|int
  405. */
  406. public static function isYearMonth($yearMonth) {
  407. return preg_match('/^\d{4}[0-1]\d$/', $yearMonth);
  408. }
  409. /**
  410. * 当前年份
  411. * @return false|string
  412. */
  413. public static function nowYear() {
  414. return date('Y', self::nowTime());
  415. }
  416. /**
  417. * 当月
  418. * @return false|string
  419. */
  420. public static function nowYearMonth() {
  421. return date('Ym', self::nowTime());
  422. }
  423. /**
  424. * 获取日期区间的值
  425. * @param array $params
  426. * @return array|bool
  427. */
  428. public static function getDateRange($params = []){
  429. $default = [
  430. 'startKey'=>'startDate',
  431. 'endKey'=>'endDate',
  432. 'value'=>null,
  433. ];
  434. $params = Tool::deepParse($params ,$default);
  435. if(!is_null($params['value'])){
  436. list($startDate, $endDate) = $params['value'];
  437. }else{
  438. $startDate = \Yii::$app->request->all($params['startKey']);
  439. $endDate = \Yii::$app->request->all($params['endKey']);
  440. $startDate = urldecode($startDate);
  441. $endDate = urldecode($endDate);
  442. }
  443. $startTime = strtotime($startDate);
  444. $endTime = strtotime($endDate);
  445. if(!$startTime || !$endTime){
  446. return false;
  447. }
  448. $monthStart = self::monthStart($startTime);
  449. return [
  450. 'startDate'=>$startDate,
  451. 'endDate'=>$endDate,
  452. 'startTime'=>$startTime,
  453. 'monthStart'=>$monthStart,
  454. 'monthStartDate'=>date(self::OCI_DATE_FORMAT_PHP, $monthStart),
  455. 'endTime'=>strtotime($endDate.'+1 day') - 1, //获取$endDate的23:59:59
  456. ];
  457. }
  458. }