LoginSignup
0
0

More than 5 years have passed since last update.

次のもっともっと交流ステーションの日付を表示するスニペット

Posted at

わたしがなるべく毎月参加しているもっともっと交流ステーションという地域イベントがあります。
せっかくなので自分のサイトに日付を表示してみました。
もっともっと交流ステーションは、基本的に毎月第一金曜日に開催されるイベントです。ただし、1月と第一金曜日が祝日の時は、開催されません。
今回のコードでは「第一金曜日が祝日」というのは5月しかないはずなので、ひとまず5月に決め打ちして処理を書いています。

内容的にはすごく単純ですが、MODxならこんな感じで簡単にPHPコード入りのページを作れますよ ということで。
なお、DateTime->modify()を指定しているので、サーバのPHPバージョンが5.3.0以降である必要があります。

NextMottoMotto
<?php
/*
* NextMottoMotto
* Author:高見知英( http://onpu-tamago.net/ )
* 次のもっともっと交流ステーションの日付を取得するスニペット
*/
  if(!isset($format)) {
    $format="Y/m/d";
  }

  $first = new DateTime();
  $date = clone $first;
  $first->modify( 'first day of this month' );
  if($date > nextFirstFriday($first)){
    $date->modify( 'first day of next month' );
    $m = $date->format('m');
    $w = $date->format('w');
    if($m == '01' || ($m == '05' && $w < 4)){
      $date->modify( 'first day of next month' );
    }
  }
  $date = nextFirstFriday($date);
  return $date->format($format);

  function nextFirstFriday($date)
  {
    $calc = clone $date;
    return $calc->modify('1 Fri');
  }
?>

で、使うときは次のように

Template,Chunk
[[NextMottoMotto]]
もしくは、
[[NextMottoMotto?format=`Y年m月d日`]] など

表示は次のようになります。

2013/07/05
formatつき
2013年07月05日

0
0
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
0
0