fork(1) download
  1. <?php
  2.  
  3. list($month, $year) = explode(" ", date('F Y', strtotime('-3 month')));
  4.  
  5. var_dump($month);
  6. var_dump($year);
  7.  
  8. // For the first example
  9. $where1 = array(
  10. 2014 => array(
  11. 'April',
  12. 'May'
  13. )
  14. );
  15.  
  16. // For the second example
  17. $where2 = array(
  18. 2013 => array(
  19. 'December'
  20. ),
  21. 2014 => array(
  22. 'January'
  23. )
  24. );
  25.  
  26. $where3 = array();
  27.  
  28. for ($prev_months = 1; $prev_months <= 5; $prev_months++) {
  29. list($month, $year) = explode(" ", date('F Y', strtotime("-$prev_months month")));
  30. $where3[$year][] = $month;
  31. }
  32.  
  33. $where4 = array();
  34.  
  35. for ($prev_months = 1; $prev_months <= 24; $prev_months++) {
  36. list($month, $year) = explode(" ", date('F Y', strtotime("-$prev_months month")));
  37. $where4[$year][] = $month;
  38. }
  39.  
  40. /**
  41. * Outputs
  42. * [where1] (month IN ('April', 'May') AND year = 2014)
  43. *
  44. * [where2] (month IN ('December') AND year = 2013) OR (month IN ('January') AND year = 2014)
  45. *
  46. * [where3] (month IN ('April', 'March', 'February', 'January') AND year = 2014) OR (month IN ('December') AND year = 2013)
  47. *
  48. * [where4] (month IN ('April', 'March', 'February', 'January') AND year = 2014) OR (month IN ('December', 'November', 'October', 'September', 'August', 'July', 'June', 'May', 'April', 'March', 'February', 'January') AND year = 2013) OR (month IN ('December', 'November', 'October', 'September', 'August', 'July', 'June', 'May') AND year = 2012)
  49. */
  50. foreach(array('where1','where2', 'where3', 'where4') as $where_name) {
  51.  
  52. $clause = '';
  53.  
  54. $where = $$where_name;
  55.  
  56. $wheres = array();
  57.  
  58. foreach($where as $year => $months) {
  59. $wheres[] = "month IN ('" . implode("', '", $months) . "') AND year = $year";
  60. }
  61.  
  62. $clause = '('.implode (') OR (', $wheres).')';
  63.  
  64. echo "[$where_name] $clause\n\n";
  65. }
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
string(8) "February"
string(4) "2014"
[where1] (month IN ('April', 'May') AND year = 2014)

[where2] (month IN ('December') AND year = 2013) OR (month IN ('January') AND year = 2014)

[where3] (month IN ('April', 'March', 'February', 'January') AND year = 2014) OR (month IN ('December') AND year = 2013)

[where4] (month IN ('April', 'March', 'February', 'January') AND year = 2014) OR (month IN ('December', 'November', 'October', 'September', 'August', 'July', 'June', 'May', 'April', 'March', 'February', 'January') AND year = 2013) OR (month IN ('December', 'November', 'October', 'September', 'August', 'July', 'June', 'May') AND year = 2012)