2015-10-20 68 views
-1

我有一個下拉列表,我想顯示當週星期一和過去8周的日期。我該怎麼做。這是我工作的代碼示例。獲取本週的每個星期一和過去8周的日期?

$maxDays=date('t'); 

for($i=0;$i<=$maxDays;$i++){ 
     echo date("m-d-Y",strtotime("monday")); 
     echo '<br>'; 
    } 
+0

你想得到最新的,和前2個月星期一的日期? –

+0

我想獲得當前每個星期一的日期和最近8周的日期 –

+0

首先你必須得到所有星期的數字,從那個你不喜歡拿星期一日期。 –

回答

0

::你的具體問題將回落到

我想獲得當月的每個星期一的日期和過去8周

因此,使用DateTimestrtotime

$date = new \DateTime; 
    $weekDay = 'Monday'; 
    $ts = strtotime('first day of next month'); 
    $date->setTimestamp($ts); 
    $month = $date->format('m'); 
    $thisMonthTs = strtotime('first day of this month'); 
    $date->setTimestamp($thisMonthTs); 
    $thisMonth = $date->format('m'); 

    while ($month >= $thisMonth) { 
     $ts = strtotime("previous $weekDay", $ts); 
     $date->setTimestamp($ts); 
     $month = $date->format('m'); 
     echo $date->format('d-m-Y'); 
     echo '<br>'; 
    } 

    for ($n = 0; $n < 7; $n++) { 
     $ts = strtotime("previous $weekDay", $ts); 
     $date->setTimestamp($ts); 
     echo $date->format('d-m-Y'); 
     echo '<br>'; 
    } 

解決方案可能b e改進了,因爲這是一個快速技巧。但所有你需要的是datetime和strtotime

+0

的示例代碼非常感謝你的解決方案 –

2

您可能想看看moment.php,這是一個PHP中的庫,用於計算所有不同類型的日期/月/年。你說

$m = new \Moment\Moment('2015-10-15T12:30:00', 'CET'); // last monday, that is 
for($i=1;$i<=8;$i++){ 
    echo $m->subtractDays($i*7)->format("m-d-Y"); // $i multiplied by seven 
    echo '<br>'; 
} 
相關問題