2012-08-23 48 views
1

返回開始日期和CURENT月的結束日期我有兩個參數的存儲過程@startdate和@EndDatePHP的查詢字符串由默認的網址

而且我有一個網址如下

的「http:// localhost/filename.php?startdate = '01 -08-2012'& enddate = '31 -08-2012'

就目前而言,每個月我都必須手動更改以獲取當月的startdate和enddate 。對於例如對於九月,我將不得不改變'01 -09-2012''31 -09-2012'

我正在尋找PHP代碼或函數,它會自動將每月的第一天作爲開始日期和最後一天該月在URL中的結束日期?

+1

你不需要在你的查詢字符串單引號的日期。你有什麼嘗試? – Raptor

+0

可能dublicate:http://stackoverflow.com/questions/2680501/how-can-i-find-the-first-and-last-date-in-a-month-using-php – Peon

回答

2

首先與前一個月的最後一天:

$current_year = date('Y'); 
$current_month = date('m'); 
$lastdateofmonth = date('t', $current_month); 
$startdate = "01-$current_month-$current_year"; 
$enddate = "$lastdateofmonth-$current_month-$current_year"; 
+0

謝謝...我什麼應該寫在URL? [localhost/filename.php?startdate = '01-08-2012'&enddate = '31-08-2012] – user1449596

+0

使用此代碼,您無需在URL中傳遞任何內容。嘗試一下。 – alfasin

+0

我應該用文本startdate和enddate替換文本「第一天」和「第二天」,因爲我的參數是startdate和enddate。我複製並粘貼了整個代碼,但它只是打印日期。 – user1449596

4

第一天:

date('Y-m-01'); //current year and month 

最後一天:

date("Y-m-t"); //current year and month and t means the last day of this month 

更改格式爲您的需求。