2015-05-22 17 views
1

兩個日期之間的所有周的開始和結束日期,我想在選擇開始日期,結束日期間,以顯示所有星期日期,所以我儘量下面的代碼問題在得到Mysql的

<? 

$signupweek='2015-05-21'; 
/*start day*/ 
for($i = 0; $i <7 ; $i++) 
{ 
$date = date('Y-m-d', strtotime("-".$i."days", strtotime($signupweek))); 
$dayName = date('D', strtotime($date)); 
if($dayName == "Sun") 
{ 
    echo "start day is ". $date."<br>"; 
} 
} 
?> 

當我在上面運行代碼它給像前7天開始到目前爲止,只有日期,但我想導致像下面

sDate = '2013-02-25', 
eDate = '2013-03-25'; 

輸出

2013-02-25     
2013-03-04     
2013-03-11     

任何想法我怎麼能使它成爲可能?你的建議是可觀的。

編輯有人答案,我得到的結果,我想,但現在我做的,像一些有點變化,現在如果我進入起始日期2015-04-01 =和END_DATE = 2015年5月1日,所以我需要一個像下面

2015-04-08 
2015-04-15 
2015-04-22 
2015-04-29 

我不需要起始日期= 2015-04-01輸出在我OUTP ut列表。它可能在下面的函數?

+0

這能否幫助你? http://stackoverflow.com/questions/712761/how-to-find-day-of-week-in-php-in-a-specific-timezone –

+0

安東尼洛佩茲:我有** start_date **和** end_date **,所以我需要**這兩個日期**之間的所有星期日期,但在您的參考鏈接中沒有任何適合我的要求的解決方案。 –

+0

'WHERE(date_field BETWEEN'2010-01-30 14:15:55'AND'2010-09-29 10:15:55')'因爲你標記爲mysql,這只是一個例子。谷歌「之間的日期MySQL」,你會發現一大堆的結果。然後是http://www.mysqltutorial.org/mysql-與更多的例子。 –

回答

1

按照編輯和OP的要求,以下新功能提供,使用戶可以指定是否包括使用的$includeFrom可選參數和$includeTo分別在最終結果的開始或結束日期:

function getWeeklyDates2($from, $to, $includeFrom = false, $includeTo = true) { 
    $from = strtotime($from); 
    $to = strtotime($to); 

    if($includeFrom===false) 
     $from = strtotime("+7 day", $from); 

    $dates = array(); 

    $current = $from; 
    while($includeTo ? $current <= $to : $current < $to) { 
     $dates[] = $current; 
     $current = strtotime("+7 day", $current); 
    } 
    return $dates; 
} 

$dates2 = getWeeklyDates2('2015-04-01', '2015-04-29', false, true); 
foreach($dates2 as $date) 
    echo date("Y-m-d", $date) ."<br />"; 
+0

someOne:是的,工作! –

1

所以你想獲得每下週日期:

<?php 
function getWeeklyDates($from, $to) { 
    $from = strtotime($from); 
    $to = strtotime($to); 

    $dates = array(); 
    $current = $from; 
    while($current <= $to) { 
     $dates[] = $current; 
     $current = strtotime("+7 day", $current); 
    } 
    return $dates; 
} 

$dates = getWeeklyDates('2013-02-25', '2013-03-25'); 
foreach($dates as $date) 
    echo date("Y-m-d", $date) ."<br />"; 
?> 
+0

雅在工作!謝謝你的努力。 –

+0

@HarshalKalavadiya關於評論StackOverflow,你可能會發現[this](http://stackoverflow.com/help/privileges/comment)和[this](http://meta.stackexchange.com/questions/43019/how - 評論 - 回覆 - 工作)有用,並歡迎您:) – someOne