Skip to content

Instantly share code, notes, and snippets.

@mlebkowski
Created February 24, 2014 11:14
Show Gist options
  • Select an option

  • Save mlebkowski/9186405 to your computer and use it in GitHub Desktop.

Select an option

Save mlebkowski/9186405 to your computer and use it in GitHub Desktop.
<?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