date.init.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.3 国产PHP开发框架 扩展类库-日期处理
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * $Author:zhuli
  10. * $Dtime:2013-5-29
  11. ***********************************************************************************/
  12. class dateInit {
  13. private $year, $month, $day; //定义年 月 日
  14. /**
  15. * 日期-设置日期
  16. * @param string $date 日期格式2010-10-10
  17. * @return
  18. */
  19. public function set_date($date = '') {
  20. if ($date !== '') {
  21. list($year, $month, $day) = explode('-', $date);
  22. $this->set_year($year);
  23. $this->set_month($month);
  24. $this->set_day($day);
  25. } else {
  26. $this->set_year(date('Y'));
  27. $this->set_month(date('m'));
  28. $this->set_day(date('d'));
  29. }
  30. }
  31. /**
  32. * 日期-增加天数
  33. * @param int $day_num 多少天
  34. * @return int
  35. */
  36. public function add_day($day_num = 1) {
  37. $day_num = (int) $day_num;
  38. $day_num = $day_num * 86400;
  39. $time = $this->get_time() + $day_num;
  40. $this->set_year(date('Y', $time));
  41. $this->set_month(date('m', $time));
  42. $this->set_day(date('d', $time));
  43. return $this->get_date();
  44. }
  45. /**
  46. * 日期-获取当月最后一天
  47. * @return int
  48. */
  49. public function get_lastday() {
  50. if($this->month==2) {
  51. $lastday = $this->is_leapyear($this->year) ? 29 : 28;
  52. } elseif($this->month==4 || $this->month==6 || $this->month==9 || $this->month==11) {
  53. $lastday = 30;
  54. } else {
  55. $lastday = 31;
  56. }
  57. return $lastday;
  58. }
  59. /**
  60. * 日期-获取星期几
  61. * @return int
  62. */
  63. public function get_week() {
  64. return date('w', $this->get_time());
  65. }
  66. /**
  67. * 日期-是否是闰年
  68. * @return int
  69. */
  70. public function is_leapyear($year) {
  71. return date('L', $year);
  72. }
  73. /**
  74. * 日期-获取当前日期
  75. * @return string 返回:2010-10-10
  76. */
  77. public function get_date() {
  78. return $this->year.'-'.$this->month.'-'.$this->day;
  79. }
  80. /**
  81. * 日期-获取当前日期-不包含年-一般用户获取生日
  82. * @return string 返回:10-10
  83. */
  84. public function get_birthday() {
  85. return $this->month.'-'.$this->day;
  86. }
  87. /**
  88. * 日期-返回时间戳
  89. * @return int
  90. */
  91. public function get_time() {
  92. return strtotime($this->get_date().' 23:59:59');
  93. }
  94. /**
  95. * 日期-计算2个日期的差值
  96. * @return int
  97. */
  98. public function get_difference($date, $new_date) {
  99. $date = strtotime($date);
  100. $new_date = strtotime($new_date);
  101. return abs(ceil(($date - $new_date)/86400));
  102. }
  103. /**
  104. * 获取星期几
  105. * @param int $week 处国人的星期,是一个数值,默认为null则使用当前时间
  106. * @return string
  107. */
  108. public static function getChinaWeek($week = null) {
  109. $week = $week ? $week : (int) date('w', time());
  110. $weekArr = array("星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
  111. return $weekArr[$week];
  112. }
  113. /**
  114. * 日期-设置年
  115. * @param string $year 年
  116. * @return
  117. */
  118. private function set_year($year) {
  119. $year = (int) $year;
  120. $this->year = ($year <= 2100 && $year >= 1970) ? $year : date('Y');
  121. }
  122. /**
  123. * 日期-设置月
  124. * @param string month 月
  125. * @return
  126. */
  127. private function set_month($month) {
  128. $month = ltrim((int) $month, '0');
  129. $this->month = ($month < 13 && $month > 0) ? $month : date('m');
  130. }
  131. /**
  132. * 日期-设置日
  133. * @param string day 天
  134. * @return
  135. */
  136. private function set_day($day) {
  137. $day = ltrim((int) $day, '0');
  138. $this->day = ($this->year && $this->month && checkdate($this->month, $day, $this->year)) ? $day : date('d');
  139. }
  140. }