Date.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. <?php
  2. namespace Util;
  3. /**
  4. * 日期时间操作类
  5. */
  6. class Date
  7. {
  8. /**
  9. * 日期的时间戳
  10. * @var integer
  11. * @access protected
  12. */
  13. protected $date;
  14. /**
  15. * 时区
  16. * @var integer
  17. * @access protected
  18. */
  19. protected $timezone;
  20. /**
  21. * 年
  22. * @var integer
  23. * @access protected
  24. */
  25. protected $year;
  26. /**
  27. * 月
  28. * @var integer
  29. * @access protected
  30. */
  31. protected $month;
  32. /**
  33. * 日
  34. * @var integer
  35. * @access protected
  36. */
  37. protected $day;
  38. /**
  39. * 时
  40. * @var integer
  41. * @access protected
  42. */
  43. protected $hour;
  44. /**
  45. * 分
  46. * @var integer
  47. * @access protected
  48. */
  49. protected $minute;
  50. /**
  51. * 秒
  52. * @var integer
  53. * @access protected
  54. */
  55. protected $second;
  56. /**
  57. * 星期的数字表示
  58. * @var integer
  59. * @access protected
  60. */
  61. protected $weekday;
  62. /**
  63. * 星期的完整表示
  64. * @var string
  65. * @access protected
  66. */
  67. protected $cWeekday;
  68. /**
  69. * 一年中的天数 0-365
  70. * @var integer
  71. * @access protected
  72. */
  73. protected $yDay;
  74. /**
  75. * 月份的完整表示
  76. * @var string
  77. * @access protected
  78. */
  79. protected $cMonth;
  80. /**
  81. * 日期CDATE表示
  82. * @var string
  83. * @access protected
  84. */
  85. protected $CDATE;
  86. /**
  87. * 日期的YMD表示
  88. * @var string
  89. * @access protected
  90. */
  91. protected $YMD;
  92. /**
  93. * 时间的输出表示
  94. * @var string
  95. * @access protected
  96. */
  97. protected $CTIME;
  98. // 星期的输出
  99. protected $Week = array("日", "一", "二", "三", "四", "五", "六");
  100. /**
  101. * 架构函数
  102. * 创建一个Date对象
  103. * @param mixed $date 日期
  104. * @static
  105. * @access public
  106. */
  107. public function __construct($date = '')
  108. {
  109. //分析日期
  110. $this->date = $this->parse($date);
  111. $this->setDate($this->date);
  112. }
  113. /**
  114. * 日期分析
  115. * 返回时间戳
  116. * @static
  117. * @access public
  118. * @param mixed $date 日期
  119. * @return string
  120. */
  121. public function parse($date)
  122. {
  123. if (is_string($date)) {
  124. if (($date == "") || strtotime($date) == -1) {
  125. //为空默认取得当前时间戳
  126. $tmpdate = time();
  127. } else {
  128. //把字符串转换成UNIX时间戳
  129. $tmpdate = strtotime($date);
  130. }
  131. } elseif (is_null($date)) {
  132. //为空默认取得当前时间戳
  133. $tmpdate = time();
  134. } elseif (is_numeric($date)) {
  135. //数字格式直接转换为时间戳
  136. $tmpdate = $date;
  137. } else {
  138. if (get_class($date) == "Date") {
  139. //如果是Date对象
  140. $tmpdate = $date->date;
  141. } else {
  142. //默认取当前时间戳
  143. $tmpdate = time();
  144. }
  145. }
  146. return $tmpdate;
  147. }
  148. /**
  149. * 验证日期数据是否有效
  150. * @access public
  151. * @param mixed $date 日期数据
  152. * @return string
  153. */
  154. public function valid($date)
  155. {
  156. }
  157. /**
  158. * 日期参数设置
  159. * @static
  160. * @access public
  161. * @param integer $date 日期时间戳
  162. * @return void
  163. */
  164. public function setDate($date)
  165. {
  166. $dateArray = getdate($date);
  167. $this->date = $dateArray[0]; //时间戳
  168. $this->second = $dateArray["seconds"]; //秒
  169. $this->minute = $dateArray["minutes"]; //分
  170. $this->hour = $dateArray["hours"]; //时
  171. $this->day = $dateArray["mday"]; //日
  172. $this->month = $dateArray["mon"]; //月
  173. $this->year = $dateArray["year"]; //年
  174. $this->weekday = $dateArray["wday"]; //星期 0~6
  175. $this->cWeekday = '星期' . $this->Week[$this->weekday]; //$dateArray["weekday"]; //星期完整表示
  176. $this->yDay = $dateArray["yday"]; //一年中的天数 0-365
  177. $this->cMonth = $dateArray["month"]; //月份的完整表示
  178. $this->CDATE = $this->format("%Y-%m-%d"); //日期表示
  179. $this->YMD = $this->format("%Y%m%d"); //简单日期
  180. $this->CTIME = $this->format("%H:%M:%S"); //时间表示
  181. return;
  182. }
  183. /**
  184. * 日期格式化
  185. * 默认返回 1970-01-01 11:30:45 格式
  186. * @access public
  187. * @param string $format 格式化参数
  188. * @return string
  189. */
  190. public function format($format = "%Y-%m-%d %H:%M:%S")
  191. {
  192. return strftime($format, $this->date);
  193. }
  194. /**
  195. * 是否为闰年
  196. * @static
  197. * @access public
  198. * @return string
  199. */
  200. public function isLeapYear($year = '')
  201. {
  202. if (empty($year)) {
  203. $year = $this->year;
  204. }
  205. return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0));
  206. }
  207. /**
  208. * 计算日期差
  209. *
  210. * w - weeks
  211. * d - days
  212. * h - hours
  213. * m - minutes
  214. * s - seconds
  215. * @static
  216. * @access public
  217. * @param mixed $date 要比较的日期
  218. * @param string $elaps 比较跨度
  219. * @return integer
  220. */
  221. public function dateDiff($date, $elaps = "d")
  222. {
  223. $__DAYS_PER_WEEK__ = (7);
  224. $__DAYS_PER_MONTH__ = (30);
  225. $__DAYS_PER_YEAR__ = (365);
  226. $__HOURS_IN_A_DAY__ = (24);
  227. $__MINUTES_IN_A_DAY__ = (1440);
  228. $__SECONDS_IN_A_DAY__ = (86400);
  229. //计算天数差
  230. $__DAYSELAPS = ($this->parse($date) - $this->date) / $__SECONDS_IN_A_DAY__;
  231. switch ($elaps) {
  232. case "y": //转换成年
  233. $__DAYSELAPS = $__DAYSELAPS / $__DAYS_PER_YEAR__;
  234. break;
  235. case "M": //转换成月
  236. $__DAYSELAPS = $__DAYSELAPS / $__DAYS_PER_MONTH__;
  237. break;
  238. case "w": //转换成星期
  239. $__DAYSELAPS = $__DAYSELAPS / $__DAYS_PER_WEEK__;
  240. break;
  241. case "h": //转换成小时
  242. $__DAYSELAPS = $__DAYSELAPS * $__HOURS_IN_A_DAY__;
  243. break;
  244. case "m": //转换成分钟
  245. $__DAYSELAPS = $__DAYSELAPS * $__MINUTES_IN_A_DAY__;
  246. break;
  247. case "s": //转换成秒
  248. $__DAYSELAPS = $__DAYSELAPS * $__SECONDS_IN_A_DAY__;
  249. break;
  250. }
  251. return $__DAYSELAPS;
  252. }
  253. /**
  254. * 人性化的计算日期差
  255. * @static
  256. * @access public
  257. * @param mixed $time 要比较的时间
  258. * @param mixed $precision 返回的精度
  259. * @return string
  260. */
  261. public function timeDiff($time, $precision = false)
  262. {
  263. if (!is_numeric($precision) && !is_bool($precision)) {
  264. static $_diff = array('y' => '年', 'M' => '个月', 'd' => '天', 'w' => '周', 's' => '秒', 'h' => '小时', 'm' => '分钟');
  265. return ceil($this->dateDiff($time, $precision)) . $_diff[$precision] . '前';
  266. }
  267. $diff = abs($this->parse($time) - $this->date);
  268. static $chunks = array(array(31536000, '年'), array(2592000, '个月'), array(604800, '周'), array(86400, '天'), array(3600, '小时'), array(60, '分钟'), array(1, '秒'));
  269. $count = 0;
  270. $since = '';
  271. for ($i = 0; $i < count($chunks); $i++) {
  272. if ($diff >= $chunks[$i][0]) {
  273. $num = floor($diff / $chunks[$i][0]);
  274. $since .= sprintf('%d' . $chunks[$i][1], $num);
  275. $diff = (int) ($diff - $chunks[$i][0] * $num);
  276. $count++;
  277. if (!$precision || $count >= $precision) {
  278. break;
  279. }
  280. }
  281. }
  282. return $since . '前';
  283. }
  284. /**
  285. * 返回周的某一天 返回Date对象
  286. * @access public
  287. * @return Date
  288. */
  289. public function getDayOfWeek($n)
  290. {
  291. $week = array(0 => 'sunday', 1 => 'monday', 2 => 'tuesday', 3 => 'wednesday', 4 => 'thursday', 5 => 'friday', 6 => 'saturday');
  292. return (new Date($week[$n]));
  293. }
  294. /**
  295. * 计算周的第一天 返回Date对象
  296. * @access public
  297. * @return Date
  298. */
  299. public function firstDayOfWeek()
  300. {
  301. return $this->getDayOfWeek(1);
  302. }
  303. /**
  304. * 计算月份的第一天 返回Date对象
  305. * @access public
  306. * @return Date
  307. */
  308. public function firstDayOfMonth()
  309. {
  310. return (new Date(mktime(0, 0, 0, $this->month, 1, $this->year)));
  311. }
  312. /**
  313. * 计算年份的第一天 返回Date对象
  314. * @access public
  315. * @return Date
  316. */
  317. public function firstDayOfYear()
  318. {
  319. return (new Date(mktime(0, 0, 0, 1, 1, $this->year)));
  320. }
  321. /**
  322. * 计算周的最后一天 返回Date对象
  323. * @access public
  324. * @return Date
  325. */
  326. public function lastDayOfWeek()
  327. {
  328. return $this->getDayOfWeek(0);
  329. }
  330. /**
  331. * 计算月份的最后一天 返回Date对象
  332. * @access public
  333. * @return Date
  334. */
  335. public function lastDayOfMonth()
  336. {
  337. return (new Date(mktime(0, 0, 0, $this->month + 1, 0, $this->year)));
  338. }
  339. /**
  340. * 计算年份的最后一天 返回Date对象
  341. * @access public
  342. * @return Date
  343. */
  344. public function lastDayOfYear()
  345. {
  346. return (new Date(mktime(0, 0, 0, 1, 0, $this->year + 1)));
  347. }
  348. /**
  349. * 计算月份的最大天数
  350. * @access public
  351. * @return integer
  352. */
  353. public function maxDayOfMonth()
  354. {
  355. $result = $this->dateDiff(strtotime($this->dateAdd(1, 'm')), 'd');
  356. return $result;
  357. }
  358. /**
  359. * 取得指定间隔日期
  360. *
  361. * yyyy - 年
  362. * q - 季度
  363. * m - 月
  364. * y - day of year
  365. * d - 日
  366. * w - 周
  367. * ww - week of year
  368. * h - 小时
  369. * n - 分钟
  370. * s - 秒
  371. * @access public
  372. * @param integer $number 间隔数目
  373. * @param string $interval 比较类型
  374. * @return Date
  375. */
  376. public function dateAdd($number = 0, $interval = "d")
  377. {
  378. $hours = $this->hour;
  379. $minutes = $this->minute;
  380. $seconds = $this->second;
  381. $month = $this->month;
  382. $day = $this->day;
  383. $year = $this->year;
  384. switch ($interval) {
  385. case "yyyy":
  386. //---Add $number to year
  387. $year += $number;
  388. break;
  389. case "q":
  390. //---Add $number to quarter
  391. $month += ($number * 3);
  392. break;
  393. case "m":
  394. //---Add $number to month
  395. $month += $number;
  396. break;
  397. case "y":
  398. case "d":
  399. case "w":
  400. //---Add $number to day of year, day, day of week
  401. $day += $number;
  402. break;
  403. case "ww":
  404. //---Add $number to week
  405. $day += ($number * 7);
  406. break;
  407. case "h":
  408. //---Add $number to hours
  409. $hours += $number;
  410. break;
  411. case "n":
  412. //---Add $number to minutes
  413. $minutes += $number;
  414. break;
  415. case "s":
  416. //---Add $number to seconds
  417. $seconds += $number;
  418. break;
  419. }
  420. return (new Date(mktime($hours,
  421. $minutes,
  422. $seconds,
  423. $month,
  424. $day,
  425. $year)));
  426. }
  427. /**
  428. * 日期数字转中文
  429. * 用于日和月、周
  430. * @static
  431. * @access public
  432. * @param integer $number 日期数字
  433. * @return string
  434. */
  435. public function numberToCh($number)
  436. {
  437. $number = intval($number);
  438. $array = array('一', '二', '三', '四', '五', '六', '七', '八', '九', '十');
  439. $str = '';
  440. if ($number == 0) {$str .= "十";}
  441. if ($number < 10) {
  442. $str .= $array[$number - 1];
  443. } elseif ($number < 20) {
  444. $str .= "十" . $array[$number - 11];
  445. } elseif ($number < 30) {
  446. $str .= "二十" . $array[$number - 21];
  447. } else {
  448. $str .= "三十" . $array[$number - 31];
  449. }
  450. return $str;
  451. }
  452. /**
  453. * 年份数字转中文
  454. * @static
  455. * @access public
  456. * @param integer $yearStr 年份数字
  457. * @param boolean $flag 是否显示公元
  458. * @return string
  459. */
  460. public function yearToCh($yearStr, $flag = false)
  461. {
  462. $array = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
  463. $str = $flag ? '公元' : '';
  464. for ($i = 0; $i < 4; $i++) {
  465. $str .= $array[substr($yearStr, $i, 1)];
  466. }
  467. return $str;
  468. }
  469. /**
  470. * 判断日期 所属 干支 生肖 星座
  471. * type 参数:XZ 星座 GZ 干支 SX 生肖
  472. * @static
  473. * @access public
  474. * @param string $type 获取信息类型
  475. * @return string
  476. */
  477. public function magicInfo($type)
  478. {
  479. $result = '';
  480. $m = $this->month;
  481. $y = $this->year;
  482. $d = $this->day;
  483. switch ($type) {
  484. case 'XZ': //星座
  485. $XZDict = array('摩羯', '宝瓶', '双鱼', '白羊', '金牛', '双子', '巨蟹', '狮子', '处女', '天秤', '天蝎', '射手');
  486. $Zone = array(1222, 122, 222, 321, 421, 522, 622, 722, 822, 922, 1022, 1122, 1222);
  487. if ((100 * $m + $d) >= $Zone[0] || (100 * $m + $d) < $Zone[1]) {
  488. $i = 0;
  489. } else {
  490. for ($i = 1; $i < 12; $i++) {
  491. if ((100 * $m + $d) >= $Zone[$i] && (100 * $m + $d) < $Zone[$i + 1]) {
  492. break;
  493. }
  494. }
  495. }
  496. $result = $XZDict[$i] . '座';
  497. break;
  498. case 'GZ': //干支
  499. $GZDict = array(
  500. array('甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸'),
  501. array('子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥'),
  502. );
  503. $i = $y - 1900 + 36;
  504. $result = $GZDict[0][$i % 10] . $GZDict[1][$i % 12];
  505. break;
  506. case 'SX': //生肖
  507. $SXDict = array('鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪');
  508. $result = $SXDict[($y - 4) % 12];
  509. break;
  510. }
  511. return $result;
  512. }
  513. /**
  514. * 友好的时间显示
  515. * @param int $sTime 待显示的时间
  516. * @param string $type 类型. normal | mohu | full | ymd | other
  517. * @param string $alt 已失效
  518. * @return string
  519. *
  520. */
  521. public function friendlyDate($type = 'normal', $alt = 'false')
  522. {
  523. $sTime = $this->date;
  524. //sTime=源时间,cTime=当前时间,dTime=时间差
  525. $cTime = time();
  526. $dTime = $cTime - $sTime;
  527. $dDay = intval(date("z", $cTime)) - intval(date("z", $sTime));
  528. //$dDay = intval($dTime/3600/24);
  529. $dYear = intval(date("Y", $cTime)) - intval(date("Y", $sTime));
  530. //normal:n秒前,n分钟前,n小时前,日期
  531. if ($type == 'normal') {
  532. if ($dTime < 60) {
  533. if ($dTime < 10) {
  534. return '刚刚';
  535. } else {
  536. return intval(floor($dTime / 10) * 10) . "秒前";
  537. }
  538. } elseif ($dTime < 3600) {
  539. return intval($dTime / 60) . "分钟前";
  540. //今天的数据.年份相同.日期相同.
  541. } elseif ($dYear == 0 && $dDay == 0) {
  542. //return intval($dTime/3600)."小时前";
  543. return '今天' . date('H:i', $sTime);
  544. } elseif ($dYear == 0) {
  545. return date("m月d日 H:i", $sTime);
  546. } else {
  547. return date("Y-m-d H:i", $sTime);
  548. }
  549. } elseif ($type == 'mohu') {
  550. if ($dTime < 60) {
  551. return $dTime . "秒前";
  552. } elseif ($dTime < 3600) {
  553. return intval($dTime / 60) . "分钟前";
  554. } elseif ($dTime >= 3600 && $dDay == 0) {
  555. return intval($dTime / 3600) . "小时前";
  556. } elseif ($dDay > 0 && $dDay <= 7) {
  557. return intval($dDay) . "天前";
  558. } elseif ($dDay > 7 && $dDay <= 30) {
  559. return intval($dDay / 7) . '周前';
  560. } elseif ($dDay > 30) {
  561. return intval($dDay / 30) . '个月前';
  562. }
  563. //full: Y-m-d , H:i:s
  564. } elseif ($type == 'full') {
  565. return date("Y-m-d , H:i:s", $sTime);
  566. } elseif ($type == 'ymd') {
  567. return date("Y-m-d", $sTime);
  568. } else {
  569. if ($dTime < 60) {
  570. return $dTime . "秒前";
  571. } elseif ($dTime < 3600) {
  572. return intval($dTime / 60) . "分钟前";
  573. } elseif ($dTime >= 3600 && $dDay == 0) {
  574. return intval($dTime / 3600) . "小时前";
  575. } elseif ($dYear == 0) {
  576. return date("Y-m-d H:i:s", $sTime);
  577. } else {
  578. return date("Y-m-d H:i:s", $sTime);
  579. }
  580. }
  581. }
  582. /**
  583. * 判断是否日期时间
  584. * @return string
  585. */
  586. public static function checkDatetime($str_time, $format = "Y-m-d H:i:s")
  587. {
  588. $unix_time = strtotime($str_time);
  589. $check_date = date($format, $unix_time);
  590. if ($check_date == $str_time) {
  591. return true;
  592. } else {
  593. return false;
  594. }
  595. }
  596. public function __toString()
  597. {
  598. return $this->format();
  599. }
  600. }