2013-02-24 50 views
2

我不完全知道從哪裏開始,因此我只舉一個例子。如何創建一個<select>字段,其​​中包含按星期排列的日期選擇。本週正開始在星期六星期日結束:在PHP中創建<select>,並按周排列日期列表

// Today is Feb 25 
<select> 
    <option value="2013-02-24" selected>Feb 24 - March 2</option> 
    <option value="2013-02-17">Feb 17 - Feb 23</option> 
    <option value="2013-02-10">Feb 10 - Feb 16</option> 
    <option value="2013-02-03">Feb 3 - Feb 9</option> 
</select> 

正如你可以在上面的例子中看到,一切都被安排在7(一週)的組。我只放置了本週最近的4周。先謝謝您的幫助!

+0

這裏的答案如何? http://stackoverflow.com/questions/8307805/how-to-get-the-date-interval-for-each-week-in-a-given-month – 2013-02-24 17:08:22

+0

RTLM:http://www.php.net/ manual/en/datetime.add.php http://www.php.net/manual/en/class.dateinterval.php – 2013-02-24 17:15:21

回答

3
<select> 
<?php 
for ($i = 0; $i < 4; $i++) { 
    echo '<option value="' . date("Y-m-d", strtotime("this sunday - $i week")) . '">' . date("M j", strtotime("this sunday - $i week")) . " - " . date("M j", strtotime("this saturday - $i week")) .'</option>'; 
} 
?> 
</select> 
+0

+1 strtotime(「this sunday」)''和strtotime(「this saturday」) ' – shnisaka 2013-02-24 17:35:04

+0

簡單地使用'strtotime()'。 – enchance 2013-02-24 17:49:25

+0

@enchance實際上有更好的使用['strtotime()'](http://php.net/manual/en/function.strtotime.php)。查看更新的答案。 – Antony 2013-02-24 17:58:40