Advertisement
jeremykendall

SQLMonthRange

Aug 19th, 2011
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.93 KB | None | 0 0
  1. Function SQLMonthRange($field, $month=0, $formatOut="M", $relative=true, $between=true) {
  2.     /*
  3.      * This is designed to be called in the middle of an SQL query to simplify
  4.      * both the readability and repeatability of the query.  The SQL query is
  5.      * assumed to be calling to capture values for a given field ($field)
  6.      * during the given month ($month).
  7.      *
  8.      * A sample call might be:
  9.      *
  10.      * $months =SQLMonthRange('COLUMN','11','M');
  11.      *
  12.      * A sample output might be:
  13.      *
  14.      * ( COLUMN BETWEEN '2007-Nov-01' AND '2007-Dec-01' )
  15.      *
  16.      * when using the default $between = true, or
  17.      *
  18.      * ( COLUMN >= '2007-Nov-01' and COLUMN < '2007-Dec-01' )
  19.      *
  20.      * when using $default = false
  21.      *
  22.      * $month is any valid (positive or negative) integer.  
  23.      * In relative mode ($relative=true), 0 is the current month.
  24.      * In absolute mode ($relative=false) 0 is December of the previous year
  25.      *
  26.      * RELATIVE MODE
  27.      * $relative (true by default) allows us to keep a sliding month scale so that
  28.      * either positive or negative values reflect the current month plus the value
  29.      * of $month.  For example, if the present month is March, a $month value of
  30.      * -2 refers to January (two months prior to March), and a $month value of 5
  31.      * refers to August (5 months after March).
  32.      *
  33.      * Be careful in absolute mode (where $relative is false).  In absolute mode,
  34.      * a $month value of -1 is NOT December.  Remember that since 1 is January, it
  35.      * follows that 0 must be December (again, this example is for absolute mode).
  36.      *
  37.      * $month =   0 ==> December of the previous year in  absolute mode ($relative=false)
  38.      * $month =  12 ==> December of the current year in absolute mode ($relative=false)
  39.      * $month = -11 ==> January of the previous year in absolute mode ($relative=false)
  40.      *
  41.      * $month =   0 ==> current month in relative mode ($relative=true)
  42.      * $month =  12 ==> 12 months ahead of current month in relative mode ($relative=true)
  43.      * $month = -11 ==> 11 months prior to current month in relative mode ($relative=true)
  44.      *  
  45.      * BETWEEN
  46.      * $between allows us to switch to a BETWEEN statement or use the >= / <=
  47.      * combination, which may be useful when comparing speed tests.  Also,
  48.      * BETWEEN is inclusive of the beginning and ending values.  That is, it does
  49.      * in fact have an implicit ">=" at the beginning value and a "<=" at the
  50.      * ending value (not just a ">" and a "<").
  51.       // http://msdn2.microsoft.com/en-us/library/aa225976(SQL.80).aspx
  52.  
  53.      */
  54.  
  55.     $field = trim($field);
  56.     $monthPlusOne = $month + 1;
  57.     $variableMonth = "+$month";
  58.     if ($relative == false) { //ABSOLUTE MONTH
  59.         $year = date("Y");
  60.         $year = date("Y", mktime(0, 0, 0, $month)); // why +1 instead of +month+1?
  61.         $monthTest = date("m", strtotime("+1 month"));
  62.         $firstday = date("Y-M-d", mktime(0, 0, 0, $month, 1));
  63.         $lastday = date("Y-M-t", mktime(0, 0, 0, $month, 1));
  64.         if ($between == true) {
  65.             $out = " ( $field BETWEEN '$firstday 00:00:00' AND '$lastday 23:59:59' ) ";
  66.         } else {
  67.             $out = " ( $field >= '$firstday 00:00:00' AND $field <= '$lastday 23:59:59' ) ";
  68.         }
  69.     } else {
  70.         if ($between == true) {
  71.             $variableMonthPlusOne = $monthPlusOne;
  72.  
  73.             $BeginningYearInQuestion = date("Y", strtotime("+$month month"));
  74.             $BeginningMonthInQuestion = date("m", strtotime("+$month month"));
  75.  
  76.             $TestEndYearInQuestion = date("Y", strtotime("+$monthPlusOne month"));
  77.             $TestEndMonthInQuestion = date("m", strtotime("+$monthPlusOne month"));
  78.             $TestEndDayInQuestion = date("d", mktime(0, 0, 0, $TestEndMonthInQuestion,
  79.                                          0, $TestEndYearInQuestion));
  80.  
  81.             // note: to get the last day of the month, go to the next month and use day "0"
  82.             // OR simply use "t" instead of "d" in the date format
  83.  
  84.             $firstday = date("Y-$formatOut-d", mktime(0, 0, 0,
  85.                              $BeginningMonthInQuestion, 1, $BeginningYearInQuestion));
  86.             $lastday = date("Y-$formatOut-d", mktime(0, 0, 0,
  87.                             $TestEndMonthInQuestion, 0, $TestEndYearInQuestion));
  88.             $out = " ( $field BETWEEN '$firstday 00:00:00' AND '$lastday 23:59:59' ) ";
  89.         } else {
  90.             $variableMonthPlusOne = "+$monthPlusOne";
  91.             $yearInQuestion = date("Y", strtotime("+$monthPlusOne month"));
  92.             $lastday = mktime(0, 0, 0, $month, 0, $yearInQuestion);
  93.             $out = " ( $field >= '"
  94.                 . date("Y-$formatOut-01", strtotime("$variableMonth month"))
  95.                 . " 00:00:00' and $field <= '"
  96.                 . date("Y-$formatOut-t", strtotime("+$variableMonth month"))
  97.                 . " 23:59:59' ) ";
  98.         }
  99.     }
  100.  
  101.     return($out);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement