0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

祝日をプログラムで判定する

Last updated at Posted at 2016-12-19

PEAR::Date_Holidays_Japan を使う。

Date_Holidays_Japan の前に依存する2つのパッケージをインストールする。
# pear install channel://pear.php.net/XML_Serializer-0.21.0 XML_Serializer
# pear install channel://pear.php.net/Date_Holidays-0.21.8 Date_Holidays
# pear install channel://pear.php.net/Date_Holidays_Japan-0.1.3 Date_Holidays_Japan

<?php
require_once 'Date/Holidays.php';

// 第2引数で年を指定
$date_holidays = DateHolidays::factory('Japan', date('Y'), 'ja-JP');
// 休日名を日本語化
$date_holidays->addTranslationFile('/usr/share/pear/data/Date_Holidays_Japan/lang/Japan/ja_JP.xml', 'ja_JP');

foreach ($date_holidays->getHolidays() as $holiday) {
  echo '<pre>';
  var_dump($holiday);
  echo '</pre><hr>';
}

$holiday->getDate()->format('%Y-%m-%d') で指定書式で取り出せるのでそいつとdate()の結果を比較すればOK

--
追記

isHoliday()なんてのがあった。わざわざdate()したのと引っ張る必要もなかった

<?php
require_once 'Date.php';
require_once 'Date/Holidays.php';

$date_holidays = DateHolidays::factory('Japan');

// 当日が休日かどうか
$date = new Date();
var_dump($date_holidays->isHoliday($date));

// 指定した日が休日かどうか
$date->setDate('2016-12-23');
var_dump($date_holidays->isHoliday($date));
0
4
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
0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?