PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Oct 15, 2018 9:05 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Get start of week
PostPosted: Sat Mar 26, 2011 8:12 am 
Offline
Forum Contributor

Joined: Sat Jan 03, 2009 4:27 pm
Posts: 148
Syntax: [ Download ] [ Hide ]
  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:

Syntax: [ Download ] [ Hide ]

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

Top
 Profile  
 
 Post subject: Re: Get start of week
PostPosted: Wed Apr 27, 2011 2:06 pm 
Offline
Forum Contributor

Joined: Sat May 03, 2008 8:43 am
Posts: 268
Aren't there people who start their week with sunday?


Top
 Profile  
 
 Post subject: Re: Get start of week
PostPosted: Fri Apr 29, 2011 1:46 am 
Offline
Forum Commoner

Joined: Tue Apr 19, 2011 7:57 am
Posts: 28
I think you could write

Syntax: [ Download ] [ Hide ]
function getStartOfWeek($format = '', $offset = '', $start = 1)
  {

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


like this:
Syntax: [ Download ] [ Hide ]
function getStartOfWeek($format = 'Y-m-d', $offset = '', $start = 1)
  {
 


I think both works, but the latter is shorter.


Top
 Profile  
 
 Post subject: Re: Get start of week
PostPosted: Fri Apr 29, 2011 10:42 am 
Offline
Forum Contributor

Joined: Sat Jan 03, 2009 4:27 pm
Posts: 148
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.


Top
 Profile  
 
 Post subject: Re: Get start of week
PostPosted: Sat Apr 30, 2011 8:49 am 
Offline
Forum Contributor

Joined: Sat May 03, 2008 8:43 am
Posts: 268
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:

Syntax: [ Download ] [ Hide ]
function getStartOfWeek($format = '', $offset = '', $startDay = 'sunday')


On the other hand, this ties it to English users. You could solve it this way:
Syntax: [ Download ] [ Hide ]
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 = '').


Top
 Profile  
 
 Post subject: Re: Get start of week
PostPosted: Sat Apr 30, 2011 12:45 pm 
Offline
Forum Contributor

Joined: Sat Jan 03, 2009 4:27 pm
Posts: 148
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.


Top
 Profile  
 
 Post subject: Re: Get start of week
PostPosted: Sat Apr 30, 2011 7:38 pm 
Offline
Forum Contributor

Joined: Sat May 03, 2008 8:43 am
Posts: 268
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.


Top
 Profile  
 
 Post subject: Re: Get start of week
PostPosted: Sun May 01, 2011 4:20 am 
Offline
Forum Contributor

Joined: Sat Jan 03, 2009 4:27 pm
Posts: 148
I see what you mean. Using names would probably be best.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group