忍者ブログ
自分なりの目線で情報を発信します。
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

Zend_Dateを使って、平日(営業日)か否かを返す処理を作りました。
祝日データは、とりあえずDBを使わずに配列に突っ込んでます。
普通に祝日テーブルを作ってそこから読み込めば済む話だと思うので、
気が向いたら修正しようと思います。
 
■仕様
受け取った日付が土日祝日以外ならTRUEを返す
(土曜は休日とみなします)
 
まだまだキレイに書けるはずですが、
とりあえずこんな感じで。
振替休日、ハッピーマンデー、
前後が祝日の平日は国民の休日、
にも対応していますが、バグあったらすんませんです~。

 
  1. /**
  2.  * 営業日(平日)か否かを返す
  3.  *
  4.  * @param Zend_Date
  5.  * @return bool
  6.  */
  7. public static function isBusinessDay($judgeDate)
  8. {
  9.     $now_ts  = time();
  10.     $workdate = new Zend_Date($now_ts, Zend_Date::TIMESTAMP);
  11.  
  12.     // 祝日の配列
  13.     //   名前
  14.     //   月
  15.     //   日
  16.     //   ハッピーマンデーになる月曜日の番号(対象月の何回目の月曜日か)
  17.     $publicHoliday    = array();
  18.     $publicHoliday['1'] = array(
  19.                               array(
  20.                                     'name' => '元日', 'month' => '1', 'day' => '1',
  21.                                     'happy_monday_num' => ''
  22.                                     ),
  23.                               array(
  24.                                     'name' => '成人の日', 'month' => '1', 'day' => '',
  25.                                     'happy_monday_num' => '2'
  26.                                     )
  27.                               );
  28.     $publicHoliday['2'] = array(
  29.                               array(
  30.                                     'name' => '建国記念の日', 'month' => '2', 'day' => '11',
  31.                                     'happy_monday_num' => ''
  32.                                     )
  33.                               );
  34.     $publicHoliday['3'] = array(
  35.                               array(
  36.                                     'name' => '春分の日', 'month' => '3', 'day' => '20',
  37.                                     'happy_monday_num' => ''
  38.                                     )
  39.                               );
  40.     $publicHoliday['4'] = array(
  41.                               array(
  42.                                     'name' => '昭和の日', 'month' => '4', 'day' => '29',
  43.                                     'happy_monday_num' => ''
  44.                                     )
  45.                               );
  46.     $publicHoliday['5'] = array(
  47.                               array(
  48.                                     'name' => '憲法記念日', 'month' => '5', 'day' => '3',
  49.                                     'happy_monday_num' => ''
  50.                                     ),
  51.                               array(
  52.                                     'name' => '昭和の日', 'month' => '5', 'day' => '4',
  53.                                     'happy_monday_num' => ''
  54.                                     ),
  55.                               array(
  56.                                     'name' => 'こどもの日', 'month' => '5', 'day' => '5',
  57.                                     'happy_monday_num' => ''
  58.                                     )
  59.                               );
  60.     $publicHoliday['7'] = array(
  61.                               array(
  62.                                     'name' => '海の日', 'month' => '7', 'day' => '',
  63.                                     'happy_monday_num' => '3'
  64.                                     )
  65.                               );
  66.     $publicHoliday['9'] = array(
  67.                               array(
  68.                                     'name' => '敬老の日', 'month' => '9', 'day' => '',
  69.                                     'happy_monday_num' => '3'
  70.                                     ),
  71.                               array(
  72.                                     'name' => '秋分の日', 'month' => '9', 'day' => '23',
  73.                                     'happy_monday_num' => ''
  74.                                    )
  75.                               );
  76.     $publicHoliday['10'] = array(
  77.                                array(
  78.                                      'name' => '体育の日', 'month' => '10', 'day' => '',
  79.                                      'happy_monday_num' => '2'
  80.                                      )
  81.                                );
  82.     $publicHoliday['11'] = array(
  83.                                array(
  84.                                      'name' => '文化の日', 'month' => '11', 'day' => '3',
  85.                                      'happy_monday_num' => ''
  86.                                      ),
  87.                                array(
  88.                                      'name' => '勤労感謝の日', 'month' => '11', 'day' => '23',
  89.                                      'happy_monday_num' => ''
  90.                                      )
  91.                                );
  92.     $publicHoliday['12'] = array(
  93.                                array(
  94.                                      'name' => '天皇誕生日', 'month' => '12', 'day' => '23',
  95.                                      'happy_monday_num' => ''
  96.                                      )
  97.                                );
  98.  
  99.     // 振替休日
  100.     $makeupHoliday = array();
  101.  
  102.     $judgeWeekdayNum = $judgeDate->get('e');
  103.     if ($judgeWeekdayNum === '0' || $judgeWeekdayNum === '6') {
  104.         // 土日は休日
  105.         return FALSE;
  106.     }
  107.  
  108.     $judgeYear     = $judgeDate->get('yyyy');
  109.     $judgeMonth    = $judgeDate->get('M');
  110.     $judgeDay      = $judgeDate->get('d');
  111.     foreach ($publicHoliday[$judgeMonth] as $key => $pubHoliday) {
  112.         if ($judgeDay === $pubHoliday['day']) {
  113.             // 判定日は祝日である
  114.             return FALSE;
  115.         }
  116.  
  117.         try {
  118.             if ($pubHoliday['happy_monday_num'] !== '') {
  119.                 // 月の最初の月曜日を探す
  120.                 $firstMonday = 0;
  121.                 $workdate->set($judgeYear, Zend_Date::YEAR);
  122.                 $workdate->set($pubHoliday['month'], Zend_Date::MONTH_SHORT);
  123.                 $workdate->set('1', Zend_Date::DAY_SHORT);
  124.                 while (TRUE) {
  125.                     if ($workdate->get('e') === '1') {
  126.                         //  月の最初の月曜日が見つかった
  127.                         $firstMonday = $workdate->get('d');
  128.                         break;
  129.                     }
  130.                     // 翌日の判定へ
  131.                     $workdate->add('1', Zend_Date::DAY_SHORT);
  132.                     continue;
  133.                 }
  134.                 // ハッピーマンデーの日付
  135.                 $happyMonday = (int)$firstMonday + 7 * ((int)$pubHoliday['happy_monday_num'] - 1);
  136.                 $happyMonday = (string)$happyMonday;
  137.                 // ハッピーマンデーを祝日用配列に記憶
  138.                 $publicHoliday[$judgeMonth][$key]['day'] = $happyMonday;
  139.                 if ($judgeDay === $happyMonday) {
  140.                     // 判定日はハッピーマンデーである
  141.                     return FALSE;
  142.                 }
  143.                 // ハッピーマンデーのため以降の処理は不要
  144.                 continue;
  145.             }
  146.  
  147.             $workdate->set($judgeYear, Zend_Date::YEAR);
  148.             $workdate->set($pubHoliday['month'], Zend_Date::MONTH_SHORT);
  149.             $workdate->set($pubHoliday['day'], Zend_Date::DAY_SHORT);
  150.             $weekdayNum = $workdate->get('e');
  151.             if ($weekdayNum === '0') {
  152.                 // この祝日は日曜日 →振替休日を求める
  153.                 while (TRUE) {
  154.                     $nextIsPubHoliday = FALSE;
  155.                     $workdate->add('1', Zend_Date::DAY_SHORT);
  156.                     $nextday = $workdate->get('d');
  157.                     foreach ($publicHoliday[$judgeMonth] as $pubHoliday2) {
  158.                         if ($nextday === $pubHoliday2['day']) {
  159.                             // 翌日は祝日である
  160.                             $nextIsPubHoliday = TRUE;
  161.                             break;
  162.                         }
  163.                     }
  164.                     if ($nextIsPubHoliday) {
  165.                         // 翌日が祝日である →さらに翌日を判定する
  166.                         continue;
  167.                     } else {
  168.                         // 翌日が祝日ではない →振替休日に設定
  169.                         $makeupHoliday[$judgeMonth][] =
  170.                             array(
  171.                                   'name' => '振替休日', 'month' => $judgeMonth, 'day' => $workdate->get('d'),
  172.                                   'happy_monday_num' => ''
  173.                                  );
  174.                         break;
  175.  
  176.                     }
  177.                 }
  178.             }
  179.         } catch (Exception $e) {
  180.             error_log('error occured. in ' . __FILE__ . ' on line ' . __LINE__);
  181.             error_log($e->getMessage());
  182.         }
  183.     }
  184.  
  185.     // 振替休日の判定
  186.     if (isset($makeupHoliday[$judgeMonth])) {
  187.         foreach ($makeupHoliday[$judgeMonth] as $mkHoliday) {
  188.             if ($judgeDay === $mkHoliday['day']) {
  189.                 // 判定日は振替休日である
  190.                 return FALSE;
  191.             }
  192.         }
  193.     }
  194.     // 判定日は国民の祝日ではないが、
  195.     // 判定日の前日及び翌日が「国民の祝日」なら休日
  196.     $judgeDayPrev = (string)((int)$judgeDay - 1);
  197.     $judgeDayNext = (string)((int)$judgeDay + 1);
  198.     foreach ($publicHoliday[$judgeMonth] as $pubHoliday) {
  199.         if ($judgeDayPrev === $pubHoliday['day']) {
  200.             foreach ($publicHoliday[$judgeMonth] as $pubHoliday2) {
  201.                 if ($judgeDayNext === $pubHoliday2['day']) {
  202.                     // 判定日の前日及び翌日が「国民の祝日」である
  203.                     return FALSE;
  204.                 }
  205.             }
  206.         }
  207.     }
  208.  
  209.     // 判定日は営業日(平日)である
  210.     return TRUE;
  211. }
     

PR
お知らせ
プロフィール
HN:
shusatoo
性別:
男性
自己紹介:
基本PHP。JavaScriptちょっとだけ。Javaをほんの少し。
おすすめ本
最新コメント
[10/09 shusatoo]
[10/09 misney]
最新トラックバック
カレンダー
03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
ブログ内検索
カウンター
アク解アナライズ
バーコード
ビジター

Copyright © [ 開発メモるアル ] All rights reserved.
Special Template : 忍者ブログ de テンプレート
Special Thanks : 忍者ブログ
Commercial message : [PR]