Date.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. /**
  3. * 洛阳赤炎鹰网络科技有限公司
  4. * https://www.cyyvip.com
  5. * Copyright (c) 2022 赤店商城 All rights reserved.
  6. */
  7. // +----------------------------------------------------------------------
  8. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  9. // +----------------------------------------------------------------------
  10. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  11. // +----------------------------------------------------------------------
  12. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  13. // +----------------------------------------------------------------------
  14. // | Author: liu21st <liu21st@gmail.com>
  15. // +----------------------------------------------------------------------
  16. namespace Org\Util;
  17. /**
  18. * 日期时间操作类
  19. * @category ORG
  20. * @package ORG
  21. * @subpackage Date
  22. * @author liu21st <liu21st@gmail.com>
  23. * @version $Id: Date.class.php 2662 2012-01-26 06:32:50Z liu21st $
  24. */
  25. class Date {
  26. /**
  27. * 日期的时间戳
  28. * @var integer
  29. * @access protected
  30. */
  31. protected $date;
  32. /**
  33. * 时区
  34. * @var integer
  35. * @access protected
  36. */
  37. protected $timezone;
  38. /**
  39. * 年
  40. * @var integer
  41. * @access protected
  42. */
  43. protected $year;
  44. /**
  45. * 月
  46. * @var integer
  47. * @access protected
  48. */
  49. protected $month;
  50. /**
  51. * 日
  52. * @var integer
  53. * @access protected
  54. */
  55. protected $day;
  56. /**
  57. * 时
  58. * @var integer
  59. * @access protected
  60. */
  61. protected $hour;
  62. /**
  63. * 分
  64. * @var integer
  65. * @access protected
  66. */
  67. protected $minute;
  68. /**
  69. * 秒
  70. * @var integer
  71. * @access protected
  72. */
  73. protected $second;
  74. /**
  75. * 星期的数字表示
  76. * @var integer
  77. * @access protected
  78. */
  79. protected $weekday;
  80. /**
  81. * 星期的完整表示
  82. * @var string
  83. * @access protected
  84. */
  85. protected $cWeekday;
  86. /**
  87. * 一年中的天数 0-365
  88. * @var integer
  89. * @access protected
  90. */
  91. protected $yDay;
  92. /**
  93. * 月份的完整表示
  94. * @var string
  95. * @access protected
  96. */
  97. protected $cMonth;
  98. /**
  99. * 日期CDATE表示
  100. * @var string
  101. * @access protected
  102. */
  103. protected $CDATE;
  104. /**
  105. * 日期的YMD表示
  106. * @var string
  107. * @access protected
  108. */
  109. protected $YMD;
  110. /**
  111. * 时间的输出表示
  112. * @var string
  113. * @access protected
  114. */
  115. protected $CTIME;
  116. // 星期的输出
  117. protected $Week = array("日","一","二","三","四","五","六");
  118. /**
  119. * 架构函数
  120. * 创建一个Date对象
  121. * @param mixed $date 日期
  122. * @static
  123. * @access public
  124. */
  125. public function __construct($date='') {
  126. //分析日期
  127. $this->date = $this->parse($date);
  128. $this->setDate($this->date);
  129. }
  130. /**
  131. * 日期分析
  132. * 返回时间戳
  133. * @static
  134. * @access public
  135. * @param mixed $date 日期
  136. * @return string
  137. */
  138. public function parse($date) {
  139. if (is_string($date)) {
  140. if (($date == "") || strtotime($date) == -1) {
  141. //为空默认取得当前时间戳
  142. $tmpdate = time();
  143. } else {
  144. //把字符串转换成UNIX时间戳
  145. $tmpdate = strtotime($date);
  146. }
  147. } elseif (is_null($date)) {
  148. //为空默认取得当前时间戳
  149. $tmpdate = time();
  150. } elseif (is_numeric($date)) {
  151. //数字格式直接转换为时间戳
  152. $tmpdate = $date;
  153. } else {
  154. if (get_class($date) == "Date") {
  155. //如果是Date对象
  156. $tmpdate = $date->date;
  157. } else {
  158. //默认取当前时间戳
  159. $tmpdate = time();
  160. }
  161. }
  162. return $tmpdate;
  163. }
  164. /**
  165. * 验证日期数据是否有效
  166. * @access public
  167. * @param mixed $date 日期数据
  168. * @return string
  169. */
  170. public function valid($date) {
  171. }
  172. /**
  173. * 日期参数设置
  174. * @static
  175. * @access public
  176. * @param integer $date 日期时间戳
  177. * @return void
  178. */
  179. public function setDate($date) {
  180. $dateArray = getdate($date);
  181. $this->date = $dateArray[0]; //时间戳
  182. $this->second = $dateArray["seconds"]; //秒
  183. $this->minute = $dateArray["minutes"]; //分
  184. $this->hour = $dateArray["hours"]; //时
  185. $this->day = $dateArray["mday"]; //日
  186. $this->month = $dateArray["mon"]; //月
  187. $this->year = $dateArray["year"]; //年
  188. $this->weekday = $dateArray["wday"]; //星期 0~6
  189. $this->cWeekday = '星期'.$this->Week[$this->weekday];//$dateArray["weekday"]; //星期完整表示
  190. $this->yDay = $dateArray["yday"]; //一年中的天数 0-365
  191. $this->cMonth = $dateArray["month"]; //月份的完整表示
  192. $this->CDATE = $this->format("%Y-%m-%d");//日期表示
  193. $this->YMD = $this->format("%Y%m%d"); //简单日期
  194. $this->CTIME = $this->format("%H:%M:%S");//时间表示
  195. return ;
  196. }
  197. /**
  198. * 日期格式化
  199. * 默认返回 1970-01-01 11:30:45 格式
  200. * @access public
  201. * @param string $format 格式化参数
  202. * @return string
  203. */
  204. public function format($format = "%Y-%m-%d %H:%M:%S") {
  205. return strftime($format, $this->date);
  206. }
  207. /**
  208. * 是否为闰年
  209. * @static
  210. * @access public
  211. * @return string
  212. */
  213. public function isLeapYear($year='') {
  214. if(empty($year)) {
  215. $year = $this->year;
  216. }
  217. return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0));
  218. }
  219. /**
  220. * 计算日期差
  221. *
  222. * w - weeks
  223. * d - days
  224. * h - hours
  225. * m - minutes
  226. * s - seconds
  227. * @static
  228. * @access public
  229. * @param mixed $date 要比较的日期
  230. * @param string $elaps 比较跨度
  231. * @return integer
  232. */
  233. public function dateDiff($date, $elaps = "d") {
  234. $__DAYS_PER_WEEK__ = (7);
  235. $__DAYS_PER_MONTH__ = (30);
  236. $__DAYS_PER_YEAR__ = (365);
  237. $__HOURS_IN_A_DAY__ = (24);
  238. $__MINUTES_IN_A_DAY__ = (1440);
  239. $__SECONDS_IN_A_DAY__ = (86400);
  240. //计算天数差
  241. $__DAYSELAPS = ($this->parse($date) - $this->date) / $__SECONDS_IN_A_DAY__ ;
  242. switch ($elaps) {
  243. case "y"://转换成年
  244. $__DAYSELAPS = $__DAYSELAPS / $__DAYS_PER_YEAR__;
  245. break;
  246. case "M"://转换成月
  247. $__DAYSELAPS = $__DAYSELAPS / $__DAYS_PER_MONTH__;
  248. break;
  249. case "w"://转换成星期
  250. $__DAYSELAPS = $__DAYSELAPS / $__DAYS_PER_WEEK__;
  251. break;
  252. case "h"://转换成小时
  253. $__DAYSELAPS = $__DAYSELAPS * $__HOURS_IN_A_DAY__;
  254. break;
  255. case "m"://转换成分钟
  256. $__DAYSELAPS = $__DAYSELAPS * $__MINUTES_IN_A_DAY__;
  257. break;
  258. case "s"://转换成秒
  259. $__DAYSELAPS = $__DAYSELAPS * $__SECONDS_IN_A_DAY__;
  260. break;
  261. }
  262. return $__DAYSELAPS;
  263. }
  264. /**
  265. * 人性化的计算日期差
  266. * @static
  267. * @access public
  268. * @param mixed $time 要比较的时间
  269. * @param mixed $precision 返回的精度
  270. * @return string
  271. */
  272. public function timeDiff( $time ,$precision=false) {
  273. if(!is_numeric($precision) && !is_bool($precision)) {
  274. static $_diff = array('y'=>'年','M'=>'个月','d'=>'天','w'=>'周','s'=>'秒','h'=>'小时','m'=>'分钟');
  275. return ceil($this->dateDiff($time,$precision)).$_diff[$precision].'前';
  276. }
  277. $diff = abs($this->parse($time) - $this->date);
  278. static $chunks = array(array(31536000,'年'),array(2592000,'个月'),array(604800,'周'),array(86400,'天'),array(3600 ,'小时'),array(60,'分钟'),array(1,'秒'));
  279. $count =0;
  280. $since = '';
  281. for($i=0;$i<count($chunks);$i++) {
  282. if($diff>=$chunks[$i][0]) {
  283. $num = floor($diff/$chunks[$i][0]);
  284. $since .= sprintf('%d'.$chunks[$i][1],$num);
  285. $diff = (int)($diff-$chunks[$i][0]*$num);
  286. $count++;
  287. if(!$precision || $count>=$precision) {
  288. break;
  289. }
  290. }
  291. }
  292. return $since.'前';
  293. }
  294. /**
  295. * 返回周的某一天 返回Date对象
  296. * @access public
  297. * @return Date
  298. */
  299. public function getDayOfWeek($n){
  300. $week = array(0=>'sunday',1=>'monday',2=>'tuesday',3=>'wednesday',4=>'thursday',5=>'friday',6=>'saturday');
  301. return (new Date($week[$n]));
  302. }
  303. /**
  304. * 计算周的第一天 返回Date对象
  305. * @access public
  306. * @return Date
  307. */
  308. public function firstDayOfWeek() {
  309. return $this->getDayOfWeek(1);
  310. }
  311. /**
  312. * 计算月份的第一天 返回Date对象
  313. * @access public
  314. * @return Date
  315. */
  316. public function firstDayOfMonth() {
  317. return (new Date(mktime(0, 0, 0,$this->month,1,$this->year )));
  318. }
  319. /**
  320. * 计算年份的第一天 返回Date对象
  321. * @access public
  322. * @return Date
  323. */
  324. public function firstDayOfYear() {
  325. return (new Date(mktime(0, 0, 0, 1, 1, $this->year)));
  326. }
  327. /**
  328. * 计算周的最后一天 返回Date对象
  329. * @access public
  330. * @return Date
  331. */
  332. public function lastDayOfWeek() {
  333. return $this->getDayOfWeek(0);
  334. }
  335. /**
  336. * 计算月份的最后一天 返回Date对象
  337. * @access public
  338. * @return Date
  339. */
  340. public function lastDayOfMonth() {
  341. return (new Date(mktime(0, 0, 0, $this->month + 1, 0, $this->year )));
  342. }
  343. /**
  344. * 计算年份的最后一天 返回Date对象
  345. * @access public
  346. * @return Date
  347. */
  348. public function lastDayOfYear() {
  349. return (new Date(mktime(0, 0, 0, 1, 0, $this->year + 1)));
  350. }
  351. /**
  352. * 计算月份的最大天数
  353. * @access public
  354. * @return integer
  355. */
  356. public function maxDayOfMonth() {
  357. $result = $this->dateDiff(strtotime($this->dateAdd(1,'m')),'d');
  358. return $result;
  359. }
  360. /**
  361. * 取得指定间隔日期
  362. *
  363. * yyyy - 年
  364. * q - 季度
  365. * m - 月
  366. * y - day of year
  367. * d - 日
  368. * w - 周
  369. * ww - week of year
  370. * h - 小时
  371. * n - 分钟
  372. * s - 秒
  373. * @access public
  374. * @param integer $number 间隔数目
  375. * @param string $interval 比较类型
  376. * @return Date
  377. */
  378. public function dateAdd($number = 0, $interval = "d") {
  379. $hours = $this->hour;
  380. $minutes = $this->minute;
  381. $seconds = $this->second;
  382. $month = $this->month;
  383. $day = $this->day;
  384. $year = $this->year;
  385. switch ($interval) {
  386. case "yyyy":
  387. //---Add $number to year
  388. $year += $number;
  389. break;
  390. case "q":
  391. //---Add $number to quarter
  392. $month += ($number*3);
  393. break;
  394. case "m":
  395. //---Add $number to month
  396. $month += $number;
  397. break;
  398. case "y":
  399. case "d":
  400. case "w":
  401. //---Add $number to day of year, day, day of week
  402. $day += $number;
  403. break;
  404. case "ww":
  405. //---Add $number to week
  406. $day += ($number*7);
  407. break;
  408. case "h":
  409. //---Add $number to hours
  410. $hours += $number;
  411. break;
  412. case "n":
  413. //---Add $number to minutes
  414. $minutes += $number;
  415. break;
  416. case "s":
  417. //---Add $number to seconds
  418. $seconds += $number;
  419. break;
  420. }
  421. return (new Date(mktime($hours,
  422. $minutes,
  423. $seconds,
  424. $month,
  425. $day,
  426. $year)));
  427. }
  428. /**
  429. * 日期数字转中文
  430. * 用于日和月、周
  431. * @static
  432. * @access public
  433. * @param integer $number 日期数字
  434. * @return string
  435. */
  436. public function numberToCh($number) {
  437. $number = intval($number);
  438. $array = array('一','二','三','四','五','六','七','八','九','十');
  439. $str = '';
  440. if($number ==0) { $str .= "十" ;}
  441. if($number < 10){
  442. $str .= $array[$number-1] ;
  443. }
  444. elseif($number < 20 ){
  445. $str .= "十".$array[$number-11];
  446. }
  447. elseif($number < 30 ){
  448. $str .= "二十".$array[$number-21];
  449. }
  450. else{
  451. $str .= "三十".$array[$number-31];
  452. }
  453. return $str;
  454. }
  455. /**
  456. * 年份数字转中文
  457. * @static
  458. * @access public
  459. * @param integer $yearStr 年份数字
  460. * @param boolean $flag 是否显示公元
  461. * @return string
  462. */
  463. public function yearToCh( $yearStr ,$flag=false ) {
  464. $array = array('零','一','二','三','四','五','六','七','八','九');
  465. $str = $flag? '公元' : '';
  466. for($i=0;$i<4;$i++){
  467. $str .= $array[substr($yearStr,$i,1)];
  468. }
  469. return $str;
  470. }
  471. /**
  472. * 判断日期 所属 干支 生肖 星座
  473. * type 参数:XZ 星座 GZ 干支 SX 生肖
  474. *
  475. * @static
  476. * @access public
  477. * @param string $type 获取信息类型
  478. * @return string
  479. */
  480. public function magicInfo($type) {
  481. $result = '';
  482. $m = $this->month;
  483. $y = $this->year;
  484. $d = $this->day;
  485. switch ($type) {
  486. case 'XZ'://星座
  487. $XZDict = array('摩羯','宝瓶','双鱼','白羊','金牛','双子','巨蟹','狮子','处女','天秤','天蝎','射手');
  488. $Zone = array(1222,122,222,321,421,522,622,722,822,922,1022,1122,1222);
  489. if((100*$m+$d)>=$Zone[0]||(100*$m+$d)<$Zone[1])
  490. $i=0;
  491. else
  492. for($i=1;$i<12;$i++){
  493. if((100*$m+$d)>=$Zone[$i]&&(100*$m+$d)<$Zone[$i+1])
  494. break;
  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. public function __toString() {
  514. return $this->format();
  515. }
  516. }