Posted By

metoikos on 06/06/10


Tagged

php date range


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

karp13


php date range function


 / Published in: PHP
 

URL: http://www.sajithmr.me/algorithm-for-range-of-dates/

  1. <?php
  2. function get_rangeof_dates($time_frame= 'all_time')
  3. {
  4. $from_date= '';
  5. $to_date= '';
  6. switch($time_frame)
  7. {
  8. case 'all_time' :
  9. $from_date= '1170-01-01 00:00:00';
  10. $to_date= '2250-12-31 23:59:59';
  11. break;
  12. case 'today' :
  13. $from_date= date('Y-m-d 00:00:00');
  14. $to_date= date('Y-m-d 23:59:59');
  15. break;
  16. case 'yesterday' :
  17. $from_date= date('Y-m-d 00:00:00', time() - 24 * 60 * 60);
  18. $to_date= date('Y-m-d 23:59:59', time() - 24 * 60 * 60);
  19. break;
  20. case 'this_week' :
  21. $from_date= date('Y-m-d 00:00:00', time() -(date('w', time()) * 60 * 60 * 24));
  22. $to_date= date('Y-m-d 23:59:59', time() +((6 - date('w', time())) * 60 * 60 * 24));
  23. break;
  24. case 'last_week' :
  25. $from_date= date('Y-m-d 00:00:00', time() -(date('w', time()) * 60 * 60 * 24) - 7 * 24 * 60 * 60);
  26. $to_date= date('Y-m-d 23:59:59', time() +((6 - date('w', time())) * 60 * 60 * 24) - 7 * 24 * 60 * 60);
  27. break;
  28. case 'last_7_days' :
  29. $additonal_query= 'created_at > ' . date('Y-m-d', time() - 7 * 24 * 60 * 60);
  30. $from_date= date('Y-m-d 00:00:00', time() - 7 * 24 * 60 * 60);
  31. $to_date= date('Y-m-d 23:59:59');
  32. break;
  33. case 'this_month' :
  34. $from_date= date('Y-m-d 00:00:00', mktime(0, 0, 0, date('m'), 1, date('Y')));
  35. $to_date= date('Y-m-d 23:59:59');
  36. break;
  37. case 'last_month' :
  38. $from_date= date('Y-m-01 00:00:00', strtotime('-1 month', strtotime(date('Y-m-d'))));
  39. $to_date= date('Y-m-d 23:59:59', strtotime('-1 day', strtotime(date('Y-m-01'))));
  40. break;
  41. case 'last_30_days' :
  42. $from_date= date('Y-m-d 00:00:00', time() - 30 * 24 * 60 * 60);
  43. $to_date= date('Y-m-d 23:59:59');
  44. break;
  45. case 'this_quarter' :
  46. $month= date('m');
  47. $year= date('Y');
  48. if($month == 1 || $month == 2 || $month == 3)
  49. {
  50. $from_date= $year . '-01-01';
  51. $to_date= $year . '-03-31';
  52. }
  53. elseif($month == 4 || $month == 5 || $month == 6)
  54. {
  55. $from_date= $year . '-04-01';
  56. $to_date= $year . '-06-30';
  57. }
  58. elseif($month == 7 || $month == 8 || $month == 9)
  59. {
  60. $from_date= $year . '-07-01';
  61. $to_date= $year . '-09-30';
  62. }
  63. else
  64. {
  65. $from_date= $year . '-10-01';
  66. $to_date= $year . '-12-31';
  67. }
  68. break;
  69. case 'last_quarter' :
  70. $month= date('m');
  71. $year= date('Y');
  72. if($month == 1 || $month == 2 || $month == 3)
  73. {
  74. $from_date=($year -1) . '-10-01';
  75. $to_date=($year -1) . '-12-31';
  76. }
  77. elseif($month == 4 || $month == 5 || $month == 6)
  78. {
  79. $from_date= $year . '-01-01';
  80. $to_date= $year . '-03-31';
  81. }
  82. elseif($month == 7 || $month == 8 || $month == 9)
  83. {
  84. $from_date= $year . '-04-01';
  85. $to_date= $year . '-06-30';
  86. }
  87. else
  88. {
  89. $from_date= $year . '-07-01';
  90. $to_date= $year . '-09-30';
  91. }
  92. break;
  93. case 'this_year' :
  94. $from_date= date('Y-01-01 00:00:00');
  95. $to_date= date('Y-12-31 23:59:59');
  96. break;
  97. case 'last_year' :
  98. $from_date= date('Y-01-01 00:00:00', mktime(0, 0, 0, 01, 01, date('Y') - 1));
  99. $to_date= date('Y-12-31 23:59:59', mktime(0, 0, 0, 12, 31, date('Y') - 1));
  100. break;
  101. }
  102. return array($from_date, $to_date);
  103. }
  104. ?>

Report this snippet  

You need to login to post a comment.