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