0

今月からのみコメントを取り込む以下の機能があります。来月からコメントを出さないようにする必要があります。

翌月を次のように定義し$to_dateてい+1ます。これを変更して、翌年になる時期を自動的に把握する、つまり13か月目に表示されないようにするにはどうすればよいですか。ありがとう

Function to only pull comments from a date range of the current month
     $from_date = date('Y-m') . '1 00:00:00';
     $to_date = date('m') . '1 00:00:00'+1;
     $all_comments = Comment::find('all', 
     array(
         'conditions'=>array('response_date_and_time >= ? AND response_date_and_time <= ?', $from_date, $to_date )
     )
);
4

2 に答える 2

2

strototimeはあなたのためにこれを行うことができます。キーワード「now+1month」を使用すると、現在の日付+1を取得し、UNIXタイムスタンプを返します。これにより、日付関数が正しい月に変換されます。02を返す必要があります

$to_date = date('m', strtotime('now + 1 month'));
于 2013-01-23T03:38:10.557 に答える
1

2つの方法があります。すでに述べたように、strtotimeを使用できます。

$to_date = date('m', strtotime('now + 1 month')) . ' 00:00:00';

しかし、モジュラス演算子について学ぶことも良い考えかもしれません%。基本的に、モジュラスは除算後に余りを返します(PHPではこれは整数ですが、他の言語(JavaScriptなど)では10進値も含まれます)。この場合、それは、月のラップが12を超えないようにするための迅速かつ簡単な方法があることを意味します。

// get the modulus
$dt = ((date('m')+1)%12);
// bit of a trick. December is the 12th month. 12 % 12 is 0, which is invalid
// so we check to see if the month is 0, and if it is we use 12 instead.
$to_date = ($dt?$dt:12) . ' 00:00:00';
于 2013-01-23T03:45:04.623 に答える