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 + 1 month”,它将当前日期 +1 并返回 unix 时间戳,日期函数将转换为正确的月份。应该返回 02

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

有两种方法。正如已经提到的,您可以使用 strtotime:

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

但我认为了解模运算符对您来说也是一个好主意%。基本上,模数将在除法后返回余数(在 PHP 中这是一个整数,在其他语言(如 JavaScript)中它也将包括任何十进制值)。在这种情况下,这意味着我们有一种快速简便的方法来确保月份换行永远不会高于 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 回答