title: PHP DateTime() class, change first day of the week to Monday
body: <p>The DateTime class in PHP (5.3+) works just great as long as the first day of the week in your country is Sunday. In the Netherlands the first day of the week is Monday and that just makes the class useless for building a calendar with week view and calculations.</p>
  
  <p>I can't seem to find an answer on Stackoverflow or the rest of the Internet on how to have DateTime act as if the first day of the week is Monday.</p>
  
  <p>I found this piece on Stackoverflow, but it doesn't fix all the ways you can get into trouble and it's not an elegant solution.</p>
  
  <pre><code>$dateTime = new DateTime('2012-05-14');
  $monday = clone $dateTime-&gt;modify(('Sunday' == $dateTime-&gt;format('l')) ? 'Monday last week' : 'Monday this week');
  </code></pre>
  
  <p>Is there a way to change this or extent DateTime? Can't imagine it's not a setting as most of Europe starts their weeks on monday.</p>
  
  <p><strong>Added:</strong>
  Posting the full calendar and functions code will not make things clearer. But here is one one line for example.
  I often have to check what the first day of the week is or  calculate from the first day of the week to a different date and time in that week. My code is getting full of these:</p>
  
  <pre><code>$startOfWeek = $date-&gt;modify(('Sunday' == $date-&gt;format('l')) ? 'Monday last week' : 'Monday this week')-&gt;modify('+3 hours')-&gt;format(DATETIME);
  </code></pre>
  
  <p>I also get an unwanted result trying to get the first full week of the month or year. As my $date object doesn't always contain the same date I have to keep checking it this way, making the code difficult to read. Having a lot more programming to do on this calendar I can't forsee where it's going to bug again.</p>
  
  <p><strong>EDIT</strong> There are some inconsistencies though. For some strange reason DateTime does get this next one right:</p>
  
  <pre><code>$test = new DateTime('2012-10-29'); // Monday
  echo $test-&gt;modify('Sunday this week')-&gt;format('Y-m-d');  // 2012-11-04
  
  // But...
  
  $test = new DateTime('2012-11-04'); // Sunday
  echo $test-&gt;modify('Monday this week')-&gt;format('Y-m-d');  // 2012-11-05 instead of 2012-10-29   
  </code></pre>
  
  <p>But I think I can make the question clearer:
  <strong>Can the DateTime() class be used with monday as the first day of the week. If not, can the class be extended to use monday as the first day of the week.</strong></p>
  
  <p><strong>UPDATE:</strong>
  Ok, I think I'm getting somewhere... I'm not a pro at coding classes..but this seems to work for the weeks. But it still needs rules for first day, second day... and also for the day name Sunday itself. I don't think this is foolproof. I would appreciate any help to fix it.</p>
  
  <pre><code>class EuroDateTime extends DateTime {
  
  // Fields
  private $weekModifiers = array (
      'this week',
      'next week',
      'previous week',
      'last week'
  );
  
  // Override "modify()"
  public function modify($string) {
  
      // Search pattern
      $pattern = '/'.implode('|', $this-&gt;weekModifiers).'/';
  
      // Change the modifier string if needed
      if ( $this-&gt;format('N') == 7 ) { // It's Sunday
          $matches = array();
          if ( preg_match( $pattern, $string, $matches )) {
              $string = str_replace($matches[0], '-7 days '.$matches[0], $string);
          }
      }
      return parent::modify($string);
  
  }
  
  }
  // This works
  $test = new EuroDateTime('2012-11-04');
  echo $test-&gt;modify('Monday this week')-&gt;format('Y-m-d');
  // And I can still concatenate calls like the DateTime class was intended
  echo $test-&gt;modify('Monday this week')-&gt;modify('+3 days')-&gt;format('Y-m-d');
  </code></pre>