2011-06-24 188 views
0
$startMonth = strtotime(2011-02-20); 
$endMonth = strtotime(2011-06-05); 
while($startMonth <= $endMonth) 
{ 
    echo date('F Y', $startMonth); 
    $startMonth = strtotime("+1 month", $startMonth); 
} 

我在使用上面的代码在开始日期和结束日期之间获取数据时遇到问题。 如果我运行这段代码,它只是能够获取02 [feb]到05 [4月]之间的数据,而06 [6月]被忽略。我只想知道我的代码有什么问题。如果我给日期2011-02-01至2011-06-01生成输出。在开始日期到结束日期之间获取数据

问题是这样的:

  • 如果我给: '2011-02-01' 到 '2011-06-01' 它的工作原理

  • 如果我给“2011-02-02 '到 '2011-02-01' 它不会工作

  • 如果我给 '2011-02-02' 到 '2011-02-03' 它的工作原理

+0

你可能想要专注于修改提取这些数据的查询,然后操纵一个太大的数据集。你能向我们展示查询吗? –

+0

它不是abt的查询部分...在代码....它无法获取日期和月...... – user813995

回答

0

更换

$startMonth = strtotime(2011-02-20); 
$endMonth = strtotime(2011-06-05); 

$startMonth = strtotime('2011-02-20'); 
$endMonth = strtotime('2011-06-05'); 

您需要明确设定日期是字符串值,因为PHP使得计算和替换2011-02-20与1989年(2011年减2减去20)

--- added ---

好吧,一个月(希望我明白你想看到什么)

<?php 
$startMonth = strtotime(date("01-n-Y",strtotime('2011-02-20'))); 
$endMonth = strtotime(date('01-n-Y',strtotime('2011-06-05'))); 
    while($startMonth <= $endMonth) 
    { 
      echo date('F Y', $startMonth); 
      $startMonth = strtotime("+1 month", $startMonth); 
    } 
?> 
+0

输出:2011年3月2011年4月2011年5月 – Greenisha

+0

我已经删除了变量,只是把日期....也没有在单引号内“' – user813995

+0

修改了我的答案。这是你想看到的吗? – Greenisha

0

您应该删除$ sign $ 2011。 $ 2011至2011年 这将是解决你的问题

0
$startMonth = strtotime(2011-02-20); 
     $endMonth = strtotime(2011-06-05); 
     while($startMonth <= $endMonth) 
     { 
       echo date('F Y', $startMonth); 
       $startMonth = strtotime("+1 month", $startMonth); 
     } 

你有额外的$,你不需要他们。

$2011-02-20 

应该

2011-02-20 
+0

对不起$不包括..... – user813995

+0

你错了,strtotime(2011) -02-20)== strtotime(1989),所以$ 2011-02-20应该是'2011-02-20',而不是2011-02-20 – Greenisha

0

如果我们认为这是一个错字$ 2011-06-20 然后

$ startMonth + 4月= 2011-06-20

2011-06-20 > 2011-06-05 

so echo not print 2011年6月

相关问题