LoginSignup
12
7

More than 5 years have passed since last update.

PHPを使って今日または次のプレミアムフレイデーを4行で取得する

Last updated at Posted at 2017-02-26

プレミアムフライデーに備えよう!Railsで次の月末の金曜日を取得する! - Qiita

PHPの日付処理使ってプレミアムフライデーからのセイクで優勝せえへん?

<?php

function current_or_next_premium_friday(): \DateTimeInterface
{
    $today = new \DateTimeImmutable('today', new \DateTimeZone('Asia/Tokyo'));
    $a = $today->modify('last Friday of');
    $b = $today->modify('last Friday of next month');
    return $a < $today ? $b : $a;
}

var_dump(current_or_next_premium_friday());

/*

object(DateTimeImmutable)#3 (3) {
  ["date"]=>
  string(26) "2017-03-31 00:00:00.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(10) "Asia/Tokyo"
}

*/

プレミアムフライデーカレンダー

<?php

function premium_fridays(\DateTimeInterface $start, $end_or_ocurrences): \DatePeriod
{
    $immutablize = function (\DateTimeInterface $d) {
        return $d instanceof \DateTime
                ? \DateTimeImmutable::createFromMutable($d)
                : $d;
    };

    $s = $immutablize($start);
    $i = \DateInterval::createFromDateString('last Friday of next month');
    $e = $end_or_ocurrences instanceof \DateTimeInterface
        ? $immutablize($end_or_ocurrences)->modify('tomorrow')
        : $end_or_ocurrences - 1;

    $a = $s->modify('last Friday of');
    $b = $s->modify('last Friday of next month');
    return new \DatePeriod($a < $s ? $b : $a, $i, $e);
}

var_dump(iterator_to_array(premium_fridays(
    new \DateTimeImmutable('2017-02-01', new \DateTimeZone('Asia/Tokyo')),
    new \DateTimeImmutable('2018-01-31', new \DateTimeZone('Asia/Tokyo'))
)));
var_dump(iterator_to_array(premium_fridays(
    new \DateTimeImmutable('2017-02-01', new \DateTimeZone('Asia/Tokyo')),
    12
)));

/*

array(12) {
  [0]=>
  object(DateTimeImmutable)#12 (3) {
    ["date"]=>
    string(26) "2017-02-24 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [1]=>
  object(DateTimeImmutable)#13 (3) {
    ["date"]=>
    string(26) "2017-03-31 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [2]=>
  object(DateTimeImmutable)#14 (3) {
    ["date"]=>
    string(26) "2017-04-28 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [3]=>
  object(DateTimeImmutable)#9 (3) {
    ["date"]=>
    string(26) "2017-05-26 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [4]=>
  object(DateTimeImmutable)#8 (3) {
    ["date"]=>
    string(26) "2017-06-30 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [5]=>
  object(DateTimeImmutable)#2 (3) {
    ["date"]=>
    string(26) "2017-07-28 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [6]=>
  object(DateTimeImmutable)#1 (3) {
    ["date"]=>
    string(26) "2017-08-25 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [7]=>
  object(DateTimeImmutable)#3 (3) {
    ["date"]=>
    string(26) "2017-09-29 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [8]=>
  object(DateTimeImmutable)#4 (3) {
    ["date"]=>
    string(26) "2017-10-27 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [9]=>
  object(DateTimeImmutable)#5 (3) {
    ["date"]=>
    string(26) "2017-11-24 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [10]=>
  object(DateTimeImmutable)#7 (3) {
    ["date"]=>
    string(26) "2017-12-29 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
  [11]=>
  object(DateTimeImmutable)#6 (3) {
    ["date"]=>
    string(26) "2018-01-26 00:00:00.000000"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(10) "Asia/Tokyo"
  }
}

*/
12
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
7