PHP Datetime() Class, Change First Day of the Week to Monday

PHP DateTime() class, change first day of the week to Monday

I found this to work, yet there are some inconsistencies in PHP's DateTime class.

If the departing date is a sunday the previous monday is not considered the same week (fixed by this class). But departing from a monday, the next sunday is considered as the same week. If they fix that in the future this class will need some additions.

class EuroDateTime extends DateTime {

// Override "modify()"
public function modify($string) {

// Change the modifier string if needed
if ( $this->format('N') == 7 ) { // It's Sunday and we're calculating a day using relative weeks
$matches = array();
$pattern = '/this week|next week|previous week|last week/i';
if ( preg_match( $pattern, $string, $matches )) {
$string = str_replace($matches[0], '-7 days '.$matches[0], $string);
}
}
return parent::modify($string);

}

}

How to set the first day of the week to Thursday in PHP

http://php.net/manual/en/datetime.formats.relative.php says that as of PHP version 5.6.23, 7.0.8 "Weeks always start on monday. Formerly, sunday would also be considered to start a week." That said, is your problem that the number of weeks returned might be incorrect depending on whether today falls on or before Thursday of the current week? Maybe try something like this:

$date = new DateTime();
$week = intval($date->format('W'));
$day = intval($date->format('N'));
echo $day < 4 ? $week-1 : $week;

If subtracting 1 isn't the answer you could play around with addition/subtraction, comparing the result with the actual answer you know to be true until you get the right formula. Hope this helps!

Get Start and End Days for a Given Week in PHP

I would take advantange of PHP's strtotime awesomeness:

function x_week_range(&$start_date, &$end_date, $date) {
$ts = strtotime($date);
$start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
$start_date = date('Y-m-d', $start);
$end_date = date('Y-m-d', strtotime('next saturday', $start));
}

Tested on the data you provided and it works. I don't particularly like the whole reference thing you have going on, though. If this was my function, I'd have it be like this:

function x_week_range($date) {
$ts = strtotime($date);
$start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
return array(date('Y-m-d', $start),
date('Y-m-d', strtotime('next saturday', $start)));
}

And call it like this:

list($start_date, $end_date) = x_week_range('2009-05-10');

I'm not a big fan of doing math for things like this. Dates are tricky and I prefer to have PHP figure it out.

Changing the first day of the week in PHP

The answer I've got for you is not PHP specific, rather it is a suggestion of how you should do it.

What you want is no different to when you must track the months of a fiscal year - the fiscal year can start on any calendar month. The way to do this is to store the day as an offset, i.e. as a day number.

Thinking about it in an OO way, you may have a data object that represents an actual calendar day (Monday through Sunday), and can hold a real date, so it represents an actual physical day. You then extend this class (inherit from it), and make a class that represents your virtual (or fiscal) day, and you add an integer property that holds its offset within your virtual/fiscal week.

So your new fiscal day object still represents an absolute date (for example Wednesday 21 October 2011), but it's offset may be zero, signifying that it is the first day in the fiscal week. This is easily extended further so that the fiscal day object can also be aware of its position within a fiscal week and a fiscal month, etc.

I hope that makes sense to you.



Related Topics



Leave a reply



Submit