redcable
asked on
Date Ranges by quarters
I am trying to set a start date and end date by the quarter.
For example, I am working on a reporting system where i need to report data for quarter 1, quarter 2, quarter 3, and quarter 4.
Quarter One - January - March
Quarter Two - April - June
Quarter Three - July - September
Quarter Four - October - December
I have for example some cases for the current month, and the previous month as shown below.
case 'this_month':
$start_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), 1, date("Y")));
$end_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), date("d"), date("Y")));
break;
case 'last_month':
$start_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
$end_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), 0, date("Y")));
break;
But now i need to add cases for each quarter and I am not sure how to actually do that so it reflects the proper quarter range, like quarter 1 always being January thru March.
Any Ideas?
For example, I am working on a reporting system where i need to report data for quarter 1, quarter 2, quarter 3, and quarter 4.
Quarter One - January - March
Quarter Two - April - June
Quarter Three - July - September
Quarter Four - October - December
I have for example some cases for the current month, and the previous month as shown below.
case 'this_month':
$start_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), 1, date("Y")));
$end_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), date("d"), date("Y")));
break;
case 'last_month':
$start_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
$end_date = date(DATE_FORMAT, mktime(0, 0, 0, date("m"), 0, date("Y")));
break;
But now i need to add cases for each quarter and I am not sure how to actually do that so it reflects the proper quarter range, like quarter 1 always being January thru March.
Any Ideas?
ASKER
not exactly what i was looking for. is there way using this format
date(DATE_FORMAT, mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
to make that mean 1/1/current year.
date(DATE_FORMAT, mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
to make that mean 1/1/current year.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry about that - I misinterpreted the code. TedInAK's right.
If you need to get from the current month to the quarter number:
$quarter = floor( $month / 4 ); // where month is 0 to 11
Then use those two functions to get the dates.
If you need to get from the current month to the quarter number:
$quarter = floor( $month / 4 ); // where month is 0 to 11
Then use those two functions to get the dates.
$periods[2] = array( 'start'=>'4/1' , 'end'=>'6/30' );
$periods[3] = array( 'start'=>'7/1' , 'end'=>'9/30' );
$periods[4] = array( 'start'=>'10/1' , 'end'=>'12/31' );
// you might want to append the year to the dates, programatically
$q = 1;
$query = "select data from table where date >= '".$periods[$q]['start'] ."' and date <= '" . $periods[$q]['end'] ."'"
That gets a single quarter of data.
If you need to group results by the quarter, you should add a column to the data (either permanently or in a temporary table), that stores the quarter. Then, all you need to do is "group by quarter". (If you're going to run this query a lot, just add the column as a permanent thing and add code to insert the proper quarter.)