Page 1 of 1
Get start of week
Posted: Sat Mar 26, 2011 8:12 am
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
Re: Get start of week
Posted: Wed Apr 27, 2011 2:06 pm
by koen.h
Aren't there people who start their week with sunday?
Re: Get start of week
Posted: Fri Apr 29, 2011 1:46 am
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.
Re: Get start of week
Posted: Fri Apr 29, 2011 10:42 am
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.
Re: Get start of week
Posted: Sat Apr 30, 2011 8:49 am
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 = '').
Re: Get start of week
Posted: Sat Apr 30, 2011 12:45 pm
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.
Re: Get start of week
Posted: Sat Apr 30, 2011 7:38 pm
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.
Re: Get start of week
Posted: Sun May 01, 2011 4:20 am
by MichaelR
I see what you mean. Using names would probably be best.