Viewed   86 times

I've seen some variants on this question but I believe this one hasn't been answered yet.

I need to get the starting date and ending date of a week, chosen by year and week number (not a date)



getStartAndEndDate($week, $year);


$return[0] = $firstDay;
$return[1] = $lastDay;

The return value will be something like an array in which the first entry is the week starting date and the second being the ending date.

OPTIONAL: while we are at it, the date format needs to be Y-n-j (normal date format, no leading zeros.

I've tried editing existing functions that almost did what I wanted but I had no luck so far.

Please help me out, thanks in advance.



Many years ago, I found this function:

function getStartAndEndDate($week, $year) {
  $dto = new DateTime();
  $dto->setISODate($year, $week);
  $ret['week_start'] = $dto->format('Y-m-d');
  $dto->modify('+6 days');
  $ret['week_end'] = $dto->format('Y-m-d');
  return $ret;

$week_array = getStartAndEndDate(52,2013);
Saturday, November 12, 2022

you need to escape the a and t as both have special meaning when used as formatting options in date()

echo date('M j at h:i a');

See it in action

Monday, October 17, 2022

You don't need to change the php.ini file if you use date_default_timezone_set(). Just set it to the timezone you will be working in.

Something like this should go in a config file or on the page where you're working with dates (if it is only one page):

Friday, December 23, 2022

Try this (pseudo-code):

// How many days gone after reference date (a known week-start date)
daysGone  = today - referenceDate;

// A new week starts after each 7 days
dayOfWeek = daysGone % 7;

// Now, we know today is which day of the week.
// We can find start & end days of this week with ease
weekStart = today - dayOfWeek;
weekEnd   = weekStart + 6;

Now, we can shorten all of this to two lines:

weekStart = today - ((today - referenceDate) % 7);
weekEnd   = weekStart + 6;

Note that we subtracted date values like integers to show algorithm. You have to write your java code properly.

Sunday, December 18, 2022

You can define two function: one for the getting the quarter of a given date, and another for getting the start and end dates of a given quarter. To get the start and end dates of the previous quarter, you would just need to subtract one from the current quarter (with some handling of first quarter).

import datetime as dt
from dateutil import parser
from dateutil.relativedelta import relativedelta

def get_quarter(date):
    Returns the calendar quarter of `date`
    return 1+(date.month-1)//3

def quarter_start_end(quarter, year=None):
    Returns datetime.daet object for the start
    and end dates of `quarter` for the input `year`
    If `year` is none, it defaults to the current
    if year is None:
        year =
    d =, 1+3*(quarter-1), 1)
    return d, d+relativedelta(months=3, days=-1)

Once these are defined, we can define a simple function to get the previous quarter.

def prev_quarter_range(date):
    Returns the start and end dates of the previous quarter
    before `date`.
    if isinstance(date, str):
        date = parser.parse(date)
    year = date.year
    q = get_quarter(date)-1
    # logic to handle the first quarter case
    if q==0:
       q = 4
       year -= 1
    return quarter_start_end(q, year)

And now you can assign the returned dates to variables

prev_q_start, prev_q_end = prev_quarter_range('2-feb-2011')


# prints:
Wednesday, October 19, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :