1

I need to get a given week's dates list when a one date of that week and the starting day is given. Starting day may be Monday, Sunday etc.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Yasitha
  • 111
  • 2
  • 3
  • 10
  • Don't know why you got a -1, this isn't a terribly bad question so here's a +1. – Endophage Jun 21 '11 at 06:49
  • possible duplicate of [Get all Work Days in a Week for a given date](http://stackoverflow.com/questions/6202576/get-all-work-days-in-a-week-for-a-given-date) – Gordon Jun 21 '11 at 07:09
  • @Endophage - It wasn't me, but it was probably downvoted because it shows no research effort or code. Questions on StackOverflow are expected to "show what you have tried". – Matt Johnson-Pint Jul 28 '13 at 18:37

2 Answers2

1

Here is a way that I think works, not sure it's the most efficient way. $weekStart should be set to the day you consider the first of the week (0 = Sunday, 1 = Monday etc) and $date is the input date.

$weekStart = 0;
$date = '2011-06-1';

$timestamp = strtotime($date);

$dayOfWeek = date('N', $timestamp);

$startDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + $weekStart, date('Y', $timestamp));
$endDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + 6 + $weekStart, date('Y', $timestamp));

echo 'Week runs from ' . date('Y-m-d', $startDate) . ' to ' . date('Y-m-d', $endDate);
Brenton Alker
  • 8,947
  • 3
  • 36
  • 37
0

Replace $dayOfWeek = date('N', $timestamp); with $dayOfWeek = date('w', $timestamp); because if using a Sunday it was returning wrong week dates. Tested using $date = '2013-07-28'.

$weekStart = 0;
$date = '2011-06-1';

$timestamp = strtotime($date);

$dayOfWeek = date('w', $timestamp);

$startDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + $weekStart, date('Y', $timestamp));
$endDate = mktime(0,0,0, date('n', $timestamp), date('j', $timestamp) - $dayOfWeek + 6 + $weekStart, date('Y', $timestamp));

echo 'Week runs from ' . date('Y-m-d', $startDate) . ' to ' . date('Y-m-d', $endDate);