2011-11-22 32 views
1

可能重複:
Get first day of week in PHP?如何在php中查找任何一週的開始?

我有這樣的腳本給就是我開始和本週結束:

$begweek = (date('l') == 'Monday')?date('Y-m-d'):date('Y-m-d',strtotime('last monday')); 
echo $begweek; 

echo "<br>"; 
$endweek = (date('l') == 'Sunday')?date('Y-m-d'):date('Y-m-d',strtotime('Sunday')); 
echo $endweek; 

我需要的是一種方式找到任何一週的開始和結束,如果我通過一個隨機日期,如$date="2011-11-15";

關於我需要在當前腳本中修改以適應此變量的想法?

感謝

+1

請參閱:http://stackoverflow.com/questions/6202576/get-all-work-days-in-a-week-for-a-given-date/6202709#6202709 - 例如:http://鍵盤.viper-7.com/82ADWj – Gordon

+0

大量重複:http://stackoverflow.com/search?q=first+day+of+week+php – Gordon

回答

2

日期函數採用一個第二參數。

THE CODE

function blah($date){ 

    $begweek = (date('l', strtotime($date)) == 'Monday')?date('Y-m-d', strtotime($date)):date('Y-m-d',strtotime('last monday', strtotime($date))); 
    echo $begweek; 

    echo "<br>"; 
    $endweek = (date('l', strtotime($date)) == 'Sunday')?date('Y-m-d', strtotime($date)):date('Y-m-d',strtotime('Sunday', strtotime($date))); 
    echo $endweek; 

} 

blah("2011-11-15"); 

輸出

2011-11-14 2011-11-20

0

date需要這是一個時間戳,以獲得第二參數日期

使用strtotime獲取時間戳。

date('l', strtotime($date)); 
0

使用「上週一」,但只是通過strtotime()隨機日期的時間戳,因爲它的第二個參數。

+0

「上一個星期一」將返回$ date前的星期一,當$ date是一個星期一 – Gordon