KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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. 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. 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. $dateTime = new DateTime('2012-05-14'); $monday = clone $dateTime->modify(('Sunday' == $dateTime->format('l')) ? 'Monday last week' : 'Monday this week'); 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. Added: 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: $startOfWeek = $date->modify(('Sunday' == $date->format('l')) ? 'Monday last week' : 'Monday this week')->modify('+3 hours')->format(DATETIME); 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. EDIT There are some inconsistencies though. For some strange reason DateTime does get this next one right: $test = new DateTime('2012-10-29'); // Monday echo $test->modify('Sunday this week')->format('Y-m-d'); // 2012-11-04 // But... $test
Tags (comma-separated)
Save Edits
Cancel