Date.php 14 KB

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