yearweek() public static method

Gets the week of the year in the same way as MYSQL's YEARWEEK(date, 0) MODE 0
public static yearweek ( string $date ) : number
$date string The date YYYY-MM-DD
return number Returns yearweek, for example: 201402
Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  * @see Iterator::current()
  * @return ChartPointInfo
  */
 public function current()
 {
     if (!isset($this->c[$this->i])) {
         $chartPoint = new ChartPointInfo($this);
         $previousPeriodDt = clone $chartPoint->dt;
         $previousPeriodDt->sub($this->getPreviousPeriodInterval());
         $ddt = clone $chartPoint->dt;
         $ddt->modify('next saturday');
         if ($ddt > $chartPoint->end) {
             $ddt = $chartPoint->end;
         }
         $chartPoint->label = $chartPoint->dt->format('M j') . ' - ' . $ddt->format('M j');
         $chartPoint->key = \Scalr_Util_DateTime::yearweek($chartPoint->dt->format('Y-m-d'));
         $chartPoint->previousPeriodKey = \Scalr_Util_DateTime::yearweek($previousPeriodDt->format('Y-m-d'));
         $chartPoint->show = $chartPoint->i % 3 == 0 ? $chartPoint->dt->format('M j') : ($chartPoint->isLastPoint && $chartPoint->i % 3 > 1 ? $ddt->format('M j') : '');
         $this->c[$this->i] = $chartPoint;
     }
     return $this->c[$this->i];
 }
Exemplo n.º 2
0
 /**
  * Constructor
  *
  * @param   ChartPeriodIterator $iterator  The iterator
  * @throws  \InvalidArgumentException
  */
 public function __construct(ChartPeriodIterator $iterator)
 {
     $this->mode = $iterator->getMode();
     $this->dt = $iterator->getIterationTimestamp();
     $this->interval = $iterator->getInterval();
     $this->i = $iterator->getIterationNumber();
     $prevStart = $iterator->getPreviousStart();
     $previousPeriodDt = clone $this->dt;
     $previousPeriodDt->sub($iterator->getPreviousPeriodInterval());
     $this->start = $iterator->getStart();
     $this->end = $iterator->getEnd();
     $this->isLastPoint = $iterator->isLastPoint();
     if ($this->mode == 'year' || $this->mode == 'custom' && $this->interval == '1 month') {
         $this->show = $this->label = $this->dt->format('M');
         $this->key = $this->dt->format('Y-m');
         $this->previousPeriodKey = $previousPeriodDt->format('Y-m');
     } elseif ($this->mode == 'quarter' || $this->mode == 'custom' && $this->interval == '1 week') {
         $ddt = clone $this->dt;
         $ddt->modify('next saturday');
         if ($ddt > $this->end) {
             $ddt = $this->end;
         }
         $this->label = $this->dt->format('M j') . ' - ' . $ddt->format('M j');
         $this->key = \Scalr_Util_DateTime::yearweek($this->dt->format('Y-m-d'));
         $this->previousPeriodKey = \Scalr_Util_DateTime::yearweek($previousPeriodDt->format('Y-m-d'));
         $this->show = $this->i % 3 == 0 ? $this->dt->format('M j') : ($this->isLastPoint && $this->i % 3 > 1 ? $ddt->format('M j') : '');
     } elseif ($this->mode == 'week') {
         $this->label = $this->dt->format('l, M j');
         $this->show = $this->dt->format('M j');
         $this->key = $this->dt->format('Y-m-d');
         $this->previousPeriodKey = $previousPeriodDt->format('Y-m-d');
     } elseif ($this->mode == 'month' || $this->mode == 'custom' && $this->interval == '1 day') {
         $this->label = $this->dt->format('M j');
         $this->key = $this->dt->format('Y-m-d');
         $this->previousPeriodKey = $previousPeriodDt->format('Y-m-d');
         $this->show = $this->i % 4 == 0 || $this->isLastPoint && $this->i % 4 > 2 ? $this->dt->format('M j') : '';
     } elseif ($this->mode == 'custom') {
         switch ($this->interval) {
             case '1 hour':
                 $h = $this->dt->format('H');
                 $this->label = $this->dt->format('l, M j, g A');
                 $this->show = $h == 0 ? $this->dt->format('M j') : ($h % 3 == 0 ? $this->dt->format('g a') : '');
                 $this->key = $this->dt->format("Y-m-d H:00:00");
                 $this->previousPeriodKey = $previousPeriodDt->format('Y-m-d H:00:00');
                 break;
             case '1 quarter':
                 //Quarter breakdown is not supported yet
                 $quarters = new Quarters(SettingEntity::getQuarters());
                 $currentPeriod = $quarters->getPeriodForDate($this->start);
                 $prevPeriod = $quarters->getPeriodForDate($prevStart);
                 $this->show = $this->label = $currentPeriod->year . ' Q' . $currentPeriod->quarter;
                 $this->key = $currentPeriod->year . '-' . $currentPeriod->quarter;
                 $this->previousPeriodKey = $prevPeriod->year . '-' . $prevPeriod->quarter;
                 break;
             case '1 year':
                 $this->show = $this->label = $this->dt->format('Y');
                 $this->key = $this->label;
                 $this->previousPeriodKey = $previousPeriodDt->format('Y');
                 break;
             default:
                 throw new \InvalidArgumentException(sprintf('Unsupported interval for custom mode %s.', $this->interval));
                 break;
         }
     } else {
         throw new \InvalidArgumentException(sprintf('Invalid mode %s', strip_tags($this->mode)));
     }
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  * @see Iterator::current()
  * @return ChartPointInfo
  */
 public function current()
 {
     if (!isset($this->c[$this->i])) {
         $chartPoint = new ChartPointInfo($this);
         $previousPeriodDt = clone $chartPoint->dt;
         $previousPeriodDt->sub($this->getPreviousPeriodInterval());
         switch ($chartPoint->interval) {
             case '1 hour':
                 $h = $chartPoint->dt->format('H');
                 $chartPoint->label = $chartPoint->dt->format('l, M j, g A');
                 $chartPoint->show = $h == 0 ? $chartPoint->dt->format('M j') : ($h % 3 == 0 ? $chartPoint->dt->format('g a') : '');
                 $chartPoint->key = $chartPoint->dt->format("Y-m-d H:00:00");
                 $chartPoint->previousPeriodKey = $previousPeriodDt->format('Y-m-d H:00:00');
                 break;
             case '1 day':
                 $chartPoint->label = $chartPoint->dt->format('M j');
                 $chartPoint->key = $chartPoint->dt->format('Y-m-d');
                 $chartPoint->previousPeriodKey = $previousPeriodDt->format('Y-m-d');
                 $chartPoint->show = $chartPoint->i % 4 == 0 || $chartPoint->isLastPoint && $chartPoint->i % 4 > 2 ? $chartPoint->dt->format('M j') : '';
                 break;
             case '1 week':
                 $ddt = clone $chartPoint->dt;
                 $ddt->modify('next saturday');
                 if ($ddt > $chartPoint->end) {
                     $ddt = $chartPoint->end;
                 }
                 $chartPoint->label = $chartPoint->dt->format('M j') . ' - ' . $ddt->format('M j');
                 $chartPoint->key = \Scalr_Util_DateTime::yearweek($chartPoint->dt->format('Y-m-d'));
                 $chartPoint->previousPeriodKey = \Scalr_Util_DateTime::yearweek($previousPeriodDt->format('Y-m-d'));
                 $chartPoint->show = $chartPoint->i % 3 == 0 ? $chartPoint->dt->format('M j') : ($chartPoint->isLastPoint && $chartPoint->i % 3 > 1 ? $ddt->format('M j') : '');
                 break;
             case '1 month':
                 $diffdays = $this->start->diff($this->end, true)->days;
                 if ($diffdays < 366) {
                     $chartPoint->show = $chartPoint->label = $chartPoint->dt->format('M');
                 } else {
                     $chartPoint->show = $chartPoint->label = $chartPoint->dt->format('M, Y');
                 }
                 $chartPoint->key = $chartPoint->dt->format('Y-m');
                 $chartPoint->previousPeriodKey = $previousPeriodDt->format('Y-m');
                 break;
             case '1 quarter':
                 //Quarter breakdown is not supported yet
                 $quarters = new Quarters(SettingEntity::getQuarters());
                 $currentPeriod = $quarters->getPeriodForDate($chartPoint->start);
                 $prevStart = $this->getPreviousStart();
                 $prevPeriod = $quarters->getPeriodForDate($prevStart);
                 $chartPoint->show = $chartPoint->label = $currentPeriod->year . ' Q' . $currentPeriod->quarter;
                 $chartPoint->key = $currentPeriod->year . '-' . $currentPeriod->quarter;
                 $chartPoint->previousPeriodKey = $prevPeriod->year . '-' . $prevPeriod->quarter;
                 break;
             case '1 year':
                 $chartPoint->show = $chartPoint->label = $chartPoint->dt->format('Y');
                 $chartPoint->key = $chartPoint->label;
                 $chartPoint->previousPeriodKey = $previousPeriodDt->format('Y');
                 break;
             default:
                 throw new \InvalidArgumentException(sprintf('Unsupported interval for custom mode %s.', $chartPoint->interval));
                 break;
         }
         $this->c[$this->i] = $chartPoint;
     }
     return $this->c[$this->i];
 }
Exemplo n.º 4
0
 /**
  * @test
  * @dataProvider providerYearweek
  */
 public function testYearweek($date)
 {
     $db = \Scalr::getDb();
     $expected = $db->GetOne("SELECT YEARWEEK(?, 0)", [$date]);
     $this->assertEquals($expected, \Scalr_Util_DateTime::yearweek($date));
 }