1

does anyone know, how to get the start and end dates of the quarters of a year? Basically I need start and end dates of the current quarter and the following 3 quarters.

I have a dropdown and it´s dates should be filled dynamically to look like this:

<select name="quarter-selection">
        <option value="2012-07-01,2012-09-31">Q3 2012</option>
        <option value="2012-10-01,2012-12-31">Q4 2012</option>
        <option value="2013-01-01,2013-03-31">Q1 2013</option>
        <option value="2013-04-01,2013-06-30">Q2 2013</option>
    </select> 

Any help highly appreciated. thx, Florian

spierala
  • 2,349
  • 3
  • 25
  • 50

2 Answers2

3

You can get it easly with this :

$q1 = array(
    'start' => date('Y-m-d', mktime(0, 0, 0, 1, 1, 2012)),
    'end' => date('Y-m-d', mktime(0, 0, 0, 3, date('t', mktime(0, 0, 0, 3, 1, 2012)), 2012))
);
$q2 = array(
    'start' => date('Y-m-d', mktime(0, 0, 0, 4, 1, 2012)),
    'end' => date('Y-m-d', mktime(0, 0, 0, 6, date('t', mktime(0, 0, 0, 3, 1, 2012)), 2012))
);
$q3 = array(
    'start' => date('Y-m-d', mktime(0, 0, 0, 7, 1, 2012)),
    'end' => date('Y-m-d', mktime(0, 0, 0, 9, date('t', mktime(0, 0, 0, 3, 1, 2012)), 2012]))
);
$q4 = array(
    'start' => date('Y-m-d', mktime(0, 0, 0, 10, 1, 2012)),
    'end' => date('Y-m-d', mktime(0, 0, 0, 12, date('t', mktime(0, 0, 0, 3, 1, 2012)), 2012))
);
PoulsQ
  • 1,936
  • 1
  • 15
  • 22
0

java.time

The troublesome old date-time classes bundled with the earliest versions of Java are now supplanted by the java.time classes.

ThreeTen-Extra

The ThreeTen-Extra project extends the java.time classes with additional functionality. This includes the Quarter and YearQuarter classes.

YearQuarter

Determining a quarter means determining a date. Determining a date means specifying a time zone. For any given moment the date varies around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" );
YearQuarter yq = YearQuarter.now( z );

You can do math, adding and subtracting.

YearQuarter yqNext = yq.plusQuarters( 1 );

LocalDate

You can interrogate for the first and last dates of that quarter, to get LocalDate objects. The LocalDate class represents a date-only value without time-of-day and without time zone.

LocalDate firstDay = yq.atDay( 1 );
LocalDate lastDay = yq.atEndOfQuarter();

To generate a String that represents the LocalDate value in standard ISO 8601 format (YYYY-MM-DD) simply call toString.

String output = firstDay.toString();

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.

Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154