dateChajian.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. class dateChajian extends Chajian
  3. {
  4. public $now;
  5. public $date;
  6. protected function initChajian()
  7. {
  8. $this->now = $this->rock->now;
  9. $this->date = $this->rock->date;
  10. }
  11. public function formatdt($dt='',$format='Y-m-d H:i:s')
  12. {
  13. return date($format,strtotime($dt));
  14. }
  15. /**
  16. 获取上月
  17. */
  18. public function lastmonth($dt, $type='Y-m')
  19. {
  20. return $this->adddate($dt,'m',-1,$type);
  21. }
  22. /**
  23. 计算时间间隔
  24. */
  25. public function datediff($type,$start,$end)
  26. {
  27. $time1 = strtotime($start);
  28. $time2 = strtotime($end);
  29. $val=0;
  30. switch($type){
  31. case 'Y'://年
  32. $Y1 = date('Y',$time1);
  33. $Y2 = date('Y',$time2);
  34. $val = $Y2 - $Y1;
  35. break;
  36. case 'm'://月份
  37. $y1 = date('Y',$time1);
  38. $y2 = date('Y',$time2);
  39. $m1 = date('m',$time1);
  40. $m2 = date('m',$time2);
  41. $y = $y1 - $y2;
  42. $mz = 0;
  43. if($y1 == $y2){
  44. $mz=$m2-$m1;
  45. }elseif($y1<$y2){
  46. $mz = 12-$m1+$m2+12*($y2-$y1-1);
  47. }else{
  48. $mz = -(12-$m2+$m1+12*($y1-$y2-1));
  49. }
  50. $val = $mz;
  51. break;
  52. case 'd'://日
  53. $dt1 = strtotime(date('Y-m-d',$time1));
  54. $dt2 = strtotime(date('Y-m-d',$time2));
  55. $time=$dt2-$dt1;
  56. $val = $time/3600/24;
  57. break;
  58. case 'H'://小时
  59. $time = $time2 - $time1;
  60. $val = floor($time/3600);
  61. break;
  62. case 'i'://分钟
  63. $time = $time2 - $time1;
  64. $val = floor($time/60);
  65. break;
  66. case 's'://秒
  67. $val = $time2 - $time1;
  68. break;
  69. }
  70. return $val;
  71. }
  72. /**
  73. 时间计算添加
  74. */
  75. public function adddate($dt,$lx,$v=0,$type='')
  76. {
  77. $time = strtotime($dt);
  78. $arrn1 = explode(' ',$dt);
  79. $arrn = explode('-',$arrn1[0]);
  80. $Y = (int)$arrn[0];
  81. $m = (int)$arrn[1];
  82. $d = (int)$arrn[2];
  83. $H=$i=$s=0;
  84. if($this->contain($dt,':')){
  85. $arrn2 = explode(':',$arrn1[1]);
  86. $H = (int)$arrn2[0];
  87. $i = (int)$arrn2[1];
  88. $s = (int)$arrn2[2];
  89. }
  90. $rval = $dt;
  91. if($type=='')$type=($H==0)?'Y-m-d':'Y-m-d H:i:s';
  92. if($type=='datetime')$type='Y-m-d H:i:s';
  93. if($v ==0)return date($type, $time);
  94. switch($lx){
  95. case 'm'://月份
  96. $time = mktime($H, $i, $s, $m+$v, $d, $Y);
  97. break;
  98. case 'Y'://年
  99. $time = mktime($H, $i, $s, $m, $d, $Y+$v);
  100. break;
  101. case 'd'://日期
  102. $time = mktime($H, $i, $s, $m, $d+$v, $Y);
  103. break;
  104. case 'H'://时
  105. $time = mktime($H+$v, $i, $s, $m, $d, $Y);
  106. break;
  107. case 'i'://分
  108. $time = mktime($H, $i+$v, $s, $m, $d, $Y);
  109. break;
  110. case 's'://秒
  111. $time = mktime($H, $i, $s+$v, $m, $d, $Y);
  112. break;
  113. }
  114. $rval = date($type,$time);
  115. return $rval;
  116. }
  117. //是否包含返回bool
  118. public function contain($str,$a)
  119. {
  120. return $this->rock->contain($str,$a);
  121. }
  122. //判断是否为空
  123. public function isempt($str)
  124. {
  125. return $this->rock->isempt($str);
  126. }
  127. public function diffstr($start, $end, $str, $lx=0, $restr='')
  128. {
  129. $time1 = strtotime($start);
  130. $time2 = strtotime($end);
  131. $sj = $time1-$time2;
  132. if($lx==1 && $sj<=0)return '';
  133. return $this->sjdate($sj, $str, $restr);
  134. }
  135. public function sjdate($sj, $str='', $restr='')
  136. {
  137. $h = $i = $s = $d = 0;
  138. $d = floor($sj/3600/24);
  139. $sj = $sj - $d * 3600 * 24;
  140. $h = floor($sj/3600);
  141. $sj = $sj - $h*3600;
  142. $i = floor($sj/60);
  143. $s = $sj - $i * 60;
  144. $str = str_replace(array('d','H','i','s'),array($d,$h,$i,$s), $str);
  145. if($restr!=''){
  146. $resta = explode(',', $restr);
  147. foreach($resta as $restas)$str = str_replace($restas,'', $str);
  148. }
  149. return $str;
  150. }
  151. public function isdate($dt)
  152. {
  153. $bo = false;
  154. if($this->isempt($dt))return $bo;
  155. $arr = explode('-', $dt);
  156. if(count($arr)>2)$bo = true;
  157. $len = strlen($dt);
  158. if($len>10){
  159. $sfm = explode(' ', $dt);
  160. if(!isset($sfm[1]))return false;
  161. $arr = explode(':', $sfm[1]);
  162. if(count($arr)<2)return false;
  163. }
  164. return $bo;
  165. }
  166. /**
  167. 返回月份最大日期
  168. */
  169. public function getenddt($month)
  170. {
  171. $month = substr($month,0,7);
  172. $max = $this->getmaxdt($month);
  173. return ''.$month.'-'.$max.'';
  174. }
  175. public function getmaxdt($dt)
  176. {
  177. $d = explode('-', $dt);
  178. $m = (int)$d[1];
  179. $y = (int)$d[0];
  180. $a = array(31,28,31,30,31,30,31,31,30,31,30,31);
  181. $d = $a[$m-1];
  182. if($y%4 == 0 && $m==2 && $y%100 != 0)$d++;
  183. return $d;
  184. }
  185. public function cnweek($date)
  186. {
  187. if(isempt($date))return '';
  188. $arr = array('日','一','二','三','四','五','六');
  189. return $arr[date('w', strtotime($date))];
  190. }
  191. //读取本周日期
  192. public function getweekarr($dt)
  193. {
  194. $w = date('w', strtotime($dt));
  195. $a = array(-6,0,-1,-2,-3,-4,-5);
  196. $oi = $a[$w];
  197. $le = $oi+7;
  198. for($j=$oi; $j<$le; $j++){
  199. $arr[] = $this->adddate($dt, 'd', $j);
  200. }
  201. return $arr;
  202. }
  203. public function getweekfirst($dt)
  204. {
  205. $arr = $this->getweekarr($dt);
  206. return $arr[0];
  207. }
  208. public function getweeklast($dt)
  209. {
  210. $arr = $this->getweekarr($dt);
  211. return $arr[6];
  212. }
  213. /**
  214. 计算返回当前间隔分析:今天 10:20
  215. */
  216. public function stringdt($dttime, $type='G H:i')
  217. {
  218. $s = '';$H=$s=$i='00';
  219. $dts= explode(' ', $dttime);
  220. $yms= explode('-', $dts[0]);
  221. $Y = $yms[0];$m = $yms[1];$d = $yms[2];
  222. $jg = $this->datediff('d', $dts[0], $this->date);
  223. $G = '';
  224. if($jg==0)$G='今天';
  225. if($jg==1)$G='昨天';
  226. if($jg==2)$G='前天';
  227. if($jg==-1)$G='明天';
  228. if($jg==-2)$G='后天';
  229. $A = $G;
  230. if($G=='')$G=substr($dts[0], 5);
  231. if($A=='')$A=$dts[0];
  232. $w = $this->cnweek($dts[0]);
  233. if(isset($dts[1])){
  234. $sjs = explode(':', $dts[1]);
  235. $H = $sjs[0];
  236. if(isset($sjs[1]))$i = $sjs[1];
  237. if(isset($sjs[2]))$s = $sjs[2];
  238. }
  239. $str = str_replace(
  240. array('A','G','H','i','s','w','Y','m','d'),
  241. array($A,$G,$H,$i,$s,$w, $Y, $m, $d),
  242. $type);
  243. return $str;
  244. }
  245. /**
  246. * 计算周期$rate:d1,d2,$dt开始时间
  247. * 返回日期,根据日期判断是不是今天
  248. */
  249. public function daterate($rate, $dt, $nowdt='')
  250. {
  251. if(isempt($rate) || isempt($dt))return false;//没有周期
  252. $dt = substr($dt,0, 10);//日期的类型
  253. if($nowdt=='')$nowdt = $this->rock->date;
  254. $nowdt = substr($nowdt, 0, 10);
  255. $jg = str_replace(array('m','d','w','y'),array('','','',''),$rate);
  256. if($jg=='')$jg='1';
  257. $jg = (int)$jg;
  258. $lx = substr($rate, 0, 1);
  259. if($lx=='d'){
  260. $jge = $this->datediff('d', $dt, $nowdt);
  261. if($jge % $jg==0 || $jge==0){
  262. return $nowdt;
  263. }
  264. }
  265. //每月
  266. if($lx=='m'){
  267. $jge = $this->datediff('m', $dt, $nowdt);
  268. if($jge % $jg==0 || $jge==0){
  269. $ndt = date('Y-m-'.substr($dt, 8).'');
  270. if($ndt==$nowdt)return $nowdt;
  271. }
  272. }
  273. //每年
  274. if($lx=='y'){
  275. $jge = $this->datediff('y', $dt, $nowdt);
  276. if($jge % $jg==0 || $jge==0){
  277. $ndt = date('Y-'.substr($dt, 5).'');
  278. if($ndt==$nowdt)return $nowdt;
  279. }
  280. }
  281. //每周
  282. if($lx=='w'){
  283. $w = (int)date('w', strtotime($nowdt));if($w==0)$w=7;//星期7
  284. if($w==$jg){
  285. return $nowdt;
  286. }
  287. }
  288. return false;
  289. }
  290. }
粤ICP备19079148号