2011-06-24 187 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月

相關問題