Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date Ranges by quarters

Posted on 2006-11-25
4
Medium Priority
?
3,772 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:redcable
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 8

Expert Comment

by:John Kawakami
ID: 18012990
$periods[1] = array( 'start'=>'1/1' , 'end'=>'3/31' );
$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.)
0
 

Author Comment

by:redcable
ID: 18013161
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.
0
 
LVL 14

Accepted Solution

by:
TedInAK earned 2000 total points
ID: 18013275
The following two functions should do what you need.  If the month is a single digit, e.g., '7', a zero is prepended - '07'.

function getQuarterStart($quarterNum) {
      return str_pad($quarterNum * 3 - 2, 2, '0', STR_PAD_LEFT) . '/01';
}

function getQuarterEnd($quarterNum) {
      return str_pad($quarterNum * 3, 2, '0', STR_PAD_LEFT) . '/' . cal_days_in_month(CAL_GREGORIAN, $quarterNum * 3, date('Y'));
}

Usage:
$qNum = 4;
echo 'Start: ' . getQuarterStart($qNum) . '/' . date('Y') . '<br>';
echo 'End: ' . getQuarterEnd($qNum) . '/' . date('Y');

Output:
   Start: 10/01/2006
   End: 12/31/2006
0
 
LVL 8

Expert Comment

by:John Kawakami
ID: 18013635
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question