Asked  7 Months ago    Answers:  5   Viewed   29 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

31

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);
Wednesday, March 31, 2021
 
talkhabi
answered 7 Months ago
96

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')       
}
Wednesday, March 31, 2021
 
CAMason
answered 7 Months ago
75

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))
);
Saturday, May 29, 2021
 
devo
answered 5 Months ago
49

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.

Thursday, June 3, 2021
 
Vlad
answered 5 Months ago
24

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.

Wednesday, July 7, 2021
 
mattltm
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :