0

I'm aware of the vast amount of date questions on SO but I am stumped at this one.

I need to run some reports and in order to do so I need to work out some dates.

The report will run on a Monday and I need to work out the dates of the Sunday the week before and the following Saturday.

e.g. The report will be run on Monday the 28th May and the dates I need are Sunday the 20th May and Saturday the 26th May.

I then need the same values but for the previous week, so Sunday the 13th May and Saturday the 19th May.

I think this part will be fine as it's just a case of getting the current date of the Monday when the report is run and and manipulating it from there.

THEN finally, the part I can't work out is the first week I mentioned, I need the corresponding dates from last year, so the dates of the Sunday and Saturday from the same week number in the previous year.

I know the date function can give you the week number but can't see how to work out the dates of the Sunday of that week and the previous Saturday based on that.

This is what I have, forgive the OTT variable names:

$today     = date('Y/m/d');

// reports run on a Monday and use last Sunday and the following Saturday's date so get yesterday's date first
$yesterday = strtotime ('-1 day', strtotime($today));
$yesterday = date ('Y/m/d', $yesterday);

// start of week (last Sunday)
$start_of_last_week       = strtotime ('-1 week', strtotime($yesterday));
$start_of_last_week       = date ('Y/m/d', $start_of_last_week);

// end of last week (the following Saturday)
$end_of_last_week       = strtotime ('+ 6 days', strtotime($start_of_last_week));
$end_of_last_week       = date ('Y/m/d', $end_of_last_week;

// start of previous week
$start_of_previous_week       = strtotime ('-1 week', strtotime($start_of_last_week));
$start_of_previous_week       = date ('Y/m/d', $start_of_previous_week);

// end of previous week
$end_of_previous_week       = strtotime ('+ 6 days', strtotime($start_of_previous_week));
$end_of_previous_week       = date ('Y/m/d', previous;



// the start of the same week last year
$start_of_last_week_last_year = strtotime ('-1 year', strtotime($start_of_last_week ));

But the above isn't right so not sure what to do next.

Any help is much appreciated.

martincarlin87
  • 10,848
  • 24
  • 98
  • 145

1 Answers1

1

You can find out the date of the Monday of a particular year and week number like this

date( 'Y-m-d', strtotime( '2012W21' )); //2011-05-23

Compound Date Formats

meouw
  • 41,754
  • 10
  • 52
  • 69
  • wow, that's excellent, never even heard of that before I don't think. Is there a way to use the day number? e.g the numeric representation of Sunday (`0`) for example? If not it's still fine as the Monday could always be manipulated using `strtotime`. – martincarlin87 May 25 '12 at 10:15
  • Yes, `date( 'Y-m-d', strtotime( '2012W210' ));` for Sunday, notice the zero after the week number, 0 - 6 for Sun to Sat of that week – meouw May 25 '12 at 12:41