date.init.php 4.0 KB

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