/seasons.php

https://github.com/cognitivestudios/Seasonally-Dynamic-Classes · PHP · 139 lines · 96 code · 23 blank · 20 comment · 5 complexity · 0a7638220098363d035d62e034b80d09 MD5 · raw file

  1. <?php
  2. /** Seasonal Dynamic CSS Class/ID Assignment
  3. * Author: Andy Smith, Cognitive Studios
  4. * Copyright: (c) 2011, Cognitive Studios
  5. *
  6. * License: CC BY 3.0
  7. * http://creativecommons.org/licenses/by/3.0/
  8. *
  9. * Disclaimer: This software is provided as-is, without any warranty of any kind.
  10. */
  11. // ------------------------------------------------------------------------
  12. // Assign a date/time to test what will results will be, or use server time.
  13. //$now = mktime(00,00,00,12,25,11);
  14. $now = time();
  15. /* Uncomment this line to write the configured time to output. Disable for live use. */
  16. // echo "Configured Time = $now ".date('m-d-Y', $now)."\n\n";
  17. /*
  18. * Configure seasons in the following array.
  19. * Each line except last ends with a comma.
  20. * new season('Long Name', 'class name', 'start month', 'start date', 'end month', 'end date'),
  21. */
  22. $seasons = array(
  23. new season('Christmas', 'christmas', '12', '01', '01', '06'),
  24. new season('Spring', 'spring', '03', '21', '06', '21'),
  25. new season('Summer', 'summer', '06', '21', '09', '21'),
  26. new season('Autumn', 'autumn', '09', '21', '12', '21'),
  27. new season('Winter', 'winter', '12', '21', '03', '21'),
  28. new season('Tax Day', 'taxday', '04', '15', '04', '15')
  29. );
  30. /*** END OF CONFIGURABLES ***/
  31. function print_active_seasons($seasons)
  32. {
  33. foreach($seasons as $s)
  34. {
  35. $s->print_active_class();
  36. }
  37. }
  38. class season
  39. {
  40. private $season_title = '';
  41. private $season_class_name = '';
  42. private $season_start;
  43. private $season_end;
  44. private $season_active = FALSE;
  45. function __construct($title = '', $class_name = '', $start_month = '', $start_date = '', $end_month = '', $end_date = '')
  46. {
  47. $this->season_title = $title;
  48. $this->season_class_name = $class_name;
  49. $this->set_active_range($start_month, $start_date, $end_month, $end_date);
  50. $this->set_active();
  51. }
  52. private function set_active_range($start_m, $start_d, $end_m, $end_d)
  53. {
  54. global $now;
  55. $year = date('Y', $now);
  56. $start_index = ($start_m * 100) + $start_d;
  57. $end_index = ($end_m * 100) + $end_d;
  58. if($start_index <= $end_index)
  59. {
  60. $this->set_start($start_m, $start_d, $year);
  61. $this->set_end($end_m, $end_d, $year);
  62. }
  63. elseif($start_index > $end_index)
  64. {
  65. $now_index = (date('m', $now) * 100) + date('d', $now);
  66. if($now_index > $end_index)
  67. {
  68. $start_y = $year;
  69. $end_y = $year + 1;
  70. $this->set_start($start_m, $start_d, $start_y);
  71. $this->set_end($end_m, $end_d, $end_y);
  72. }
  73. elseif($now_index <= $end_index)
  74. {
  75. $start_y = $year - 1;
  76. $end_y = $year;
  77. $this->set_start($start_m, $start_d, $start_y);
  78. $this->set_end($end_m, $end_d, $end_y);
  79. }
  80. }
  81. }
  82. private function set_start($m, $d, $y)
  83. {
  84. $start = mktime(00, 00, 00, $m, $d, $y);
  85. $this->season_start = $start;
  86. }
  87. private function set_end($m, $d, $y)
  88. {
  89. $end = mktime(23, 59, 59, $m, $d, $y);
  90. $this->season_end = $end;
  91. }
  92. private function set_active()
  93. {
  94. global $now;
  95. if($now >= $this->season_start && $now <= $this->season_end)
  96. {
  97. $this->season_active = TRUE;
  98. }
  99. }
  100. private function get_class_name()
  101. {
  102. return $this->season_class_name;
  103. }
  104. private function is_active()
  105. {
  106. return $this->season_active;
  107. }
  108. public function print_active_class()
  109. {
  110. if($this->is_active())
  111. {
  112. echo $this->get_class_name().' ';
  113. }
  114. }
  115. }
  116. ?>