2012-01-02 82 views
0

我需要幫忙的「本週」完整的日期範圍,格式如下:年月日獲得前一週的日期範圍PHP

我已經成功地能得到「這個月」完整日期範圍,但不是「這一週「的全日期範圍。

這是我爲 「這個月」 代碼:

//Functions for later use 
function firstOfMonth() { 
    return date("Y-m-d", strtotime(date('m').'/01/'.date('Y').' 00:00:00')); 
} 

function lastOfMonth() { 
    return date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')))); 
} 

//Setup the date_range variables 
$date_start = firstOfMonth(); 
$date_end = lastOfMonth(); 

任何幫助,不勝感激!

回答

6

就這麼你知道,你的「第一個月」代碼是錯誤的。如果你在八月,它會給你七月八號到八月七號的範圍。這樣做時使用正確的d/m/Y格式。

至於本週,試試這個:

$start_week = strtotime("last monday midnight"); 
$end_week = strtotime("+1 week",$start_week); 

$start_week = date("Y/m/d",$start_week); 
$end_week = date("Y/m/d",$end_week); 
+1

如果它現在的星期一現在不行,這僅適用於上週嗎?如果它的星期二不會在上週一給昨天? – 2012-08-06 17:01:58

+0

^是的。在上週的星期一,你需要做'$ thisWeeksMonday = strtotime(「上週一午夜」); $ lastWeeksMonday = strtotime(' - 1周',$ thisWeeksMonday);'。當然,你需要檢查今天是否也是星期一。另外,答案中有一個錯字。 – musicliftsme 2014-02-26 20:29:42

+0

@FranVerona感謝您的編輯。我有點驚訝,1)我犯了這些錯誤,2)他們很久沒有被編輯了! – 2014-08-28 15:22:34

2

雷頓埃弗森你是正確的。我認爲正確的格式應該是:

strtotime('-2 Monday');