LoginSignup
1
2

More than 5 years have passed since last update.

DateTimeクラスで平日のリスト

Posted at

自分用メモです。
PHPで用意されてるDateTimeクラスだと自然言語的な感じで欲しい日付が取れたりする。しかし、平日が欲しいときに平日くださいと入れても出てこない。なんともならず苦肉の策です。もうちょっと簡単にならないかなー。

DateTime.php
$date_list = [];
    $week_day = ['日', '月', '火', '水', '木', '金', '土'];
for ($i=0; $i < 31; $i++) {
    $date = new DateTime('+'.(String)$i.'days');
    if ((int)$date->format('w') !== 0 && (int)$date->format('w') !== 6) {
        $year = $date->format('Y');
        $month = $date->format('m');
        $day = $date->format('d');
        $youbi = $date->format('w');

        $date_list[] = $year.'年'.$month.'月'.$day.'日'.$week_day[$youbi].'曜日';
    }
}

1
2
1

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
1
2