Asked  2 Years ago    Answers:  5   Viewed   72 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)

example:

input:

getStartAndEndDate($week, $year);

output:

$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.

 Answers

1

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);
print_r($week_array);
Saturday, November 12, 2022
2

You could also take a look at the DatePeriod class:

$period = new DatePeriod(
     new DateTime('2010-10-01'),
     new DateInterval('P1D'),
     new DateTime('2010-10-05')
);

Which should get you an array with DateTime objects.

To iterate

foreach ($period as $key => $value) {
    //$value->format('Y-m-d')       
}
Friday, November 18, 2022
5

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))
);
Friday, September 23, 2022
 
3

You need to call DateInterval::format() to display that difference as a string.

echo $diff->format('%d days');

See the manual for all of the available formatting options.

Saturday, September 24, 2022
 
emilian
 
4

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
    year.
    """
    if year is None:
        year = dt.datetime.now().year
    d = dt.date(year, 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')

print(prev_q_start)
print(prev_q_end)

# prints:
2010-10-01
2010-12-31
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 :
 

Browse Other Code Languages