Date.class.php 15 KB

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