/DDDateHelper.php

https://bitbucket.org/jwerner/yii-ddautofilter · PHP · 194 lines · 164 code · 3 blank · 27 comment · 24 complexity · 11add37450978b9f33dfc19359d18e9f MD5 · raw file

  1. <?php
  2. /*
  3. * DDDateHelper class file
  4. *
  5. * @author Joachim Werner <joachim.werner@diggin-data.de>
  6. /**
  7. * Some date functions
  8. *
  9. * @author Joachim Werner <joachim.werner@diggin-data.de>
  10. */
  11. class DDDateHelper
  12. {
  13. // {{{ *** Date Methods ***
  14. // {{{ Days
  15. // {{{ DayTomorrow
  16. function DayTomorrow($ts=null)
  17. {
  18. if($ts==null)
  19. $ts=mktime();
  20. return strtotime("+1 day", $ts);
  21. }
  22. // }}}
  23. // {{{ DayYesterday
  24. function DayYesterday($ts=null)
  25. {
  26. if($ts==null)
  27. $ts=mktime();
  28. return strtotime("-1 day", $ts);
  29. } // }}}
  30. // {{{ DaysRange
  31. function DaysRange($start, $end, $workingDaysOnly=false)
  32. {
  33. list($year,$month,$day) = explode('-',$start);
  34. $start = mktime(0,0,0,$month,$day,$year);
  35. list($year,$month,$day) = explode('-',$end);
  36. $end = mktime(23,59,59,$month,$day,$year);
  37. $tmp = array();
  38. $current = $start;
  39. while($current<=$end) {
  40. if($workingDaysOnly==true and in_array(date("w",$current),array(1,2,3,4,5)))
  41. $tmp[] = strtotime(date("Y-m-d",$current));
  42. $current = $current + +60*60*24;
  43. }
  44. return $tmp;
  45. } // }}}
  46. // }}}
  47. // {{{ Weeks
  48. function WeekFirstDay($ts=null)
  49. {
  50. if($ts==null)
  51. $ts=mktime();
  52. $day = date("w",$ts);
  53. if($day==1)
  54. return strtotime(date("Y-m-d",$ts));
  55. else
  56. return strtotime(date("Y-m-d",strtotime("last monday",$ts)));
  57. }
  58. function WeekLastDay($ts=null)
  59. {
  60. if($ts==null)
  61. $ts=mktime();
  62. $day = date("w",$ts);
  63. if($day==6)
  64. return strtotime(date("Y-m-d",$ts));
  65. else
  66. return strtotime(date("Y-m-d",strtotime("sunday",$ts)));
  67. }
  68. function WeekThis($ts=null)
  69. {
  70. if($ts==null)
  71. $ts=mktime();
  72. return array(self::WeekFirstDay($ts),self::WeekLastDay($ts));
  73. }
  74. function WeekRelative($ts=null,$relative=0)
  75. {
  76. if($ts==null)
  77. $ts=mktime();
  78. $tsDiff = $relative*86400*7;
  79. return self::WeekThis($ts+$tsDiff);
  80. }
  81. // }}}
  82. // {{{ Months
  83. function MonthFirstDay($ts=null)
  84. {
  85. if($ts==null)
  86. $ts=mktime();
  87. if(date("j",$ts)==1 )
  88. return strtotime(date("Y-m-d",$ts));
  89. else
  90. return strtotime(date("Y-m-d",mktime(0,0,0,date("n",$ts),1,date("Y",$ts))));
  91. }
  92. function MonthLastDay($ts=null)
  93. {
  94. if($ts==null)
  95. $ts=mktime();
  96. $nextMonth = mktime(0,0,0,date("n",$ts)+1,1,date("Y",$ts));
  97. return strtotime(date("Y-m-d", strtotime("-1 day", $nextMonth)));
  98. }
  99. function MonthThis($ts=null)
  100. {
  101. if($ts==null)
  102. $ts=mktime();
  103. return array(self::MonthFirstDay($ts),self::MonthLastDay($ts));
  104. }
  105. function MonthRelative($ts=null,$relative=0)
  106. {
  107. if($ts==null)
  108. $ts=mktime();
  109. $tsNew = mktime(0, 0, 0, date("n",$ts)+$relative,1,date("Y",$ts));
  110. return self::MonthThis($tsNew);
  111. }
  112. // }}}
  113. // {{{ Quarters
  114. function QuarterFirstDay($ts=null)
  115. {
  116. if($ts==null)
  117. $ts=mktime();
  118. $month = date("n", $ts);
  119. if($month>=1 and $month<=3)
  120. return strtotime(date("Y-m-d", mktime(0,0,0,1,1,date("Y",$ts))));
  121. elseif($month>=4 and $month<=6)
  122. return strtotime(date("Y-m-d", mktime(0,0,0,4,1,date("Y",$ts))));
  123. elseif($month>=7 and $month<=9)
  124. return strtotime(date("Y-m-d", mktime(0,0,0,7,1,date("Y",$ts))));
  125. else
  126. return strtotime(date("Y-m-d", mktime(0,0,0,10,1,date("Y",$ts))));
  127. }
  128. function QuarterLastDay($ts=null)
  129. {
  130. $month = date("n", $ts);
  131. if($month>=1 and $month<=3)
  132. return strtotime(date("Y-m-d", mktime(0,0,0,3,31,date("Y",$ts))));
  133. elseif($month>=4 and $month<=6)
  134. return strtotime(date("Y-m-d", mktime(0,0,0,6,30,date("Y",$ts))));
  135. elseif($month>=7 and $month<=9)
  136. return strtotime(date("Y-m-d", mktime(0,0,0,9,30,date("Y",$ts))));
  137. else
  138. return strtotime(date("Y-m-d", mktime(0,0,0,12,31,date("Y",$ts))));
  139. }
  140. function QuarterThis($ts=null)
  141. {
  142. if($ts==null)
  143. $ts=mktime();
  144. return array(self::QuarterFirstDay($ts),self::QuarterLastDay($ts));
  145. }
  146. function QuarterRelative($ts=null,$relative=0)
  147. {
  148. if($ts==null)
  149. $ts=mktime();
  150. $month = date("n", $ts)+$relative*3;
  151. return self::QuarterThis(mktime(0,0,0,$month,1,date("Y",$ts)));
  152. }
  153. // }}}
  154. // {{{ Years
  155. function YearFirstDay($ts=null)
  156. {
  157. if($ts==null)
  158. $ts=mktime();
  159. return strtotime(date("Y-m-d",mktime(0,0,0,1,1,date("Y",$ts))));
  160. }
  161. function YearLastDay($ts=null)
  162. {
  163. if($ts==null)
  164. $ts=mktime();
  165. return strtotime(date("Y-m-d H:i:s",mktime(23,59,59,12,31,date("Y",$ts))));
  166. }
  167. function YearThis($ts=null)
  168. {
  169. if($ts==null)
  170. $ts=mktime();
  171. return array(self::YearFirstDay($ts),self::YearLastDay($ts));
  172. }
  173. function YearToDate($ts=null)
  174. {
  175. if($ts==null)
  176. $ts=mktime();
  177. return array(strtotime(date("Y-m-d H:i:s", mktime(0,0,0,1,1,date("Y")))),strtotime(date("Y-m-d H:i:s",$ts)));
  178. }
  179. function YearRelative($ts=null,$relative=0)
  180. {
  181. if($ts==null)
  182. $ts=mktime();
  183. $tsNew = mktime(0,0,0,date("n",$ts),date("j",$ts),date("Y",$ts)+$relative);
  184. return self::YearThis($tsNew);
  185. }
  186. // }}}
  187. // }}}
  188. }
  189. /* vim: set ai expandtab tabstop=4 shiftwidth=4 softtabstop=4 fdm=marker fdc=4: */
  190. ?>