1

I have a stored procedure with two parameters @startdate and @enddate

And I have a URL as below

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

As it stands, every month I have to change this manually to get startdate and enddate of the present month. For e.g. for September, i will have to change '01-09-2012' '31-09-2012'

I'm looking for php code or function which will take automatically first day of every month as startdate and last day of the the month as enddate in the url?

antoniom
  • 3,143
  • 1
  • 37
  • 53
user1449596
  • 309
  • 4
  • 9
  • 28
  • 1
    you don't need to single-quote the date in your query string. And what have you tried ? – Raptor Aug 23 '12 at 07:11
  • Possible dublicate: http://stackoverflow.com/questions/2680501/how-can-i-find-the-first-and-last-date-in-a-month-using-php – Peon Aug 23 '12 at 07:22

2 Answers2

4

First day:

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

Last day:

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

Change the formatting for your needs.

Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
2

First and last day of the previous month:

$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";
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Thanks...What I shall write in url? [localhost/filename.php?startdate='01-08-2012'&enddate='31-08-2012] – user1449596 Aug 23 '12 at 08:09
  • with this code you don't need to pass anything in the URL. try it. – Nir Alfasi Aug 23 '12 at 08:17
  • shall i replace your text "first day" and "second day" with text startdate and enddate because my parameters are startdate and enddate. I copied and pasted entire your code but it just prints the date. – user1449596 Aug 23 '12 at 08:35
  • I changed the code, now it'll set the parameters like you need (startdate, enddate). It would be easier if you showed me your code and what would you expect as an output. – Nir Alfasi Aug 23 '12 at 08:41
  • Now it worked. Many Thanks..As I'm new to coding..i'm not able to understand easily..i simply didn't know to add $startdate and $enddate. After you changed it for me, it worked. – user1449596 Aug 23 '12 at 08:47