LoginSignup
23
9

More than 5 years have passed since last update.

PHPでプレミアムフライデー(月末の金曜日)を求める

Posted at

いよいよプレミアムフライデーですね!
働いていますが何か?

月末の金曜日

最後の曜日を計算する書式は 'last dayname of' です。
PHP: 相対的な書式 - Manual

$d = new \DateTimeImmutable('last fri of');
echo $d->format('Y-m-d') . PHP_EOL;

/*
2017-02-24
 */

年間の月末の金曜日

$begin = new \DateTimeImmutable('2017-01');
$end   = new \DateTimeImmutable('2017-12');

$iterator = new \DatePeriod(
    $begin->modify('last fri of'),
    \DateInterval::createFromDateString('last fri of next month'),
    $end->modify('first day of next month')
);

foreach($iterator as $date) {
    echo $date->format('Y-m-d') . PHP_EOL;
}

/*
2017-01-27
2017-02-24
2017-03-31
2017-04-28
2017-05-26
2017-06-30
2017-07-28
2017-08-25
2017-09-29
2017-10-27
2017-11-24
2017-12-29
 */

よい週末を

23
9
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
23
9