Created
February 24, 2014 11:14
-
-
Save mlebkowski/9186405 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class SeasonalityFactor | |
| { | |
| const FORMAT = 'M'; | |
| /** | |
| * @var array | |
| */ | |
| private $modifiers = array (); | |
| public function __construct(array $modifiers) | |
| { | |
| $date = new \DateTime("1 January"); | |
| do | |
| { | |
| $current = strtolower($date->format(self::FORMAT)); | |
| $this->modifiers[$current] = isset($modifiers[$current]) ? (float) $modifiers[$current] : 1; | |
| $date->modify("next month"); | |
| } | |
| while ($date->format('M') != "Jan"); | |
| } | |
| /** | |
| * @param \DateTime $dateStart | |
| * @param \DateTime $dateEnd | |
| * | |
| * @return float | |
| */ | |
| public function getFactor(\DateTime $dateStart, \DateTime $dateEnd) | |
| { | |
| $factor = 0; | |
| $cnt = 0; | |
| for ($date = clone $dateStart; $date <= $dateEnd; $date->modify("next day")) | |
| { | |
| $month = strtolower($date->format(self::FORMAT)); | |
| $factor += $this->modifiers[$month]; | |
| $cnt++; | |
| } | |
| return $cnt ? $factor / $cnt : 1; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment