Get start of week

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Get start of week

Post by MichaelR »

Code: Select all

  function getStartOfWeek($format = '', $offset = '', $start = 'Monday')
  {

    if (empty($format))
    {
      $format = 'Y-m-d';
    }

    $date = array(

      0 => 'Sunday',
      1 => 'Monday',
      2 => 'Tuesday',
      3 => 'Wednesday',
      4 => 'Thursday',
      5 => 'Friday',
      6 => 'Saturday',

    );

    if (!in_array($start, $date))
    {
      $start = 'Monday';
    }

    $day = date('l') == $start ? strtotime('today ' . $offset) : strtotime('last ' . $start . $offset);

    return date($format, $day);

  }
Examples:

Code: Select all


  // Today is Monday 21 March 2011

  echo getStartOfWeek(); // 2011-03-21
  echo getStartOfWeek('d/m/Y'); // 21/03/2011
  echo getStartOfWeek('', '+ 1 week'); // 28/03/2011

  // Today is Sunday 27 March 2011 

  echo getStartOfWeek(); // 2011-03-21
  echo getStartOfWeek('d/m/Y'); // 21/03/2011
  echo getStartOfWeek('', '+ 1 week'); // 28/03/2011

  // Today is Monday 21 March 2011 and the week starts on a Sunday 

  echo getStartOfWeek('', '', 7); // 2011-03-20
  echo getStartOfWeek('d/m/Y', '', 7); // 20/03/2011
  echo getStartOfWeek('', '+ 1 week', 7); // 27/03/2011

Last edited by MichaelR on Sun May 01, 2011 4:26 am, edited 1 time in total.
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Get start of week

Post by koen.h »

Aren't there people who start their week with sunday?
eivind
Forum Commoner
Posts: 28
Joined: Tue Apr 19, 2011 7:57 am

Re: Get start of week

Post by eivind »

I think you could write

Code: Select all

function getStartOfWeek($format = '', $offset = '', $start = 1)
  {

    if (empty($format))
    {
      $format = 'Y-m-d';
    }
like this:

Code: Select all

function getStartOfWeek($format = 'Y-m-d', $offset = '', $start = 1)
  {
  
I think both works, but the latter is shorter.
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: Get start of week

Post by MichaelR »

koen.h wrote:Aren't there people who start their week with sunday.
That's what the third parameter is for; to set the day which starts the week. It defaults to 1 (Monday).

eivind: Originally I did have that but decided to change it to make it quicker for those who might want to use the default format but want to change the offset and/or the start day.
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Get start of week

Post by koen.h »

MichaelR wrote:That's what the third parameter is for; to set the day which starts the week. It defaults to 1 (Monday).
If the week starts on sunday, for those people sunday will be 1. If I were one of those I would prefer your constructor as this:

Code: Select all

function getStartOfWeek($format = '', $offset = '', $startDay = 'sunday')
On the other hand, this ties it to English users. You could solve it this way:

Code: Select all

function getStartOfWeek($format = '', $offset = '', Day $startDayOfWeek) {}

interface Day {
    public function toEnglish();
}
This of course makes it less simple than your original and suggests problems (translation) you may know you'll not run into (eg only used in house). On the other hand, this road leads to an object Week, with a method getStartDayOfWeek($format = '', $offset = '').
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: Get start of week

Post by MichaelR »

I don't really see what difference it makes using 1 as Monday and 7 as Sunday. If the person using the function cares that much about which integer refers to which month then they're more than welcome to change it. There's no point in making it unnecessarily complicated.
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Get start of week

Post by koen.h »

MichaelR wrote:I don't really see what difference it makes using 1 as Monday and 7 as Sunday. If the person using the function cares that much about which integer refers to which month then they're more than welcome to change it. There's no point in making it unnecessarily complicated.
The point is that it is not clear. If you find my approach unnecessary difficult I would suggest leaving out weekdays as numbers and use the names of the days instead. If you need to look into the code in the function to see what you should fill in as an argument it is unnecessarily complicated.
MichaelR
Forum Contributor
Posts: 148
Joined: Sat Jan 03, 2009 3:27 pm

Re: Get start of week

Post by MichaelR »

I see what you mean. Using names would probably be best.
Post Reply