PHP DateTime() 类,将一周的第一天更改为星期一 [英] PHP DateTime() class, change first day of the week to Monday

查看:31
本文介绍了PHP DateTime() 类,将一周的第一天更改为星期一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只要您所在国家/地区的一周的第一天是星期日,PHP (5.3+) 中的 DateTime 类就可以很好地工作.在荷兰,一周的第一天是星期一,这使得该类无法用于构建带有周视图和计算的日历.

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.

我似乎无法在 Stackoverflow 或互联网上的其他地方找到关于如何让 DateTime 表现得好像一周的第一天是星期一的答案.

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.

我在 Stackoverflow 上找到了这篇文章,但它并没有解决所有可能遇到麻烦的方式,也不是一个优雅的解决方案.

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');

有没有办法改变这个或范围DateTime?无法想象这不是一个环境,因为欧洲大部分地区从星期一开始他们的一周.

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);

我在尝试获取一个月或一年的第一个完整周时也得到了不需要的结果.由于我的 $date 对象并不总是包含相同的日期,因此我必须以这种方式不断检查它,从而使代码难以阅读.在这个日历上有更多的编程要做,我无法预测它会在哪里再次出错.

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 虽然存在一些不一致之处.出于某种奇怪的原因 DateTime 确实让下一个正确:

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 = new DateTime('2012-11-04'); // Sunday
echo $test->modify('Monday this week')->format('Y-m-d');  // 2012-11-05 instead of 2012-10-29   

但我想我可以把问题说得更清楚:DateTime() 类是否可以将星期一用作一周的第一天.如果没有,是否可以将课程扩展为使用星期一作为一周的第一天.

But I think I can make the question clearer: 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.

更新:好吧,我想我到了某个地方......我不是编码课程的专家......但这似乎可以持续数周.但它仍然需要第一天、第二天的规则......以及星期日本身的名称.我不认为这是万无一失的.如果您能帮助我解决此问题,我将不胜感激.

UPDATE: 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.

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->weekModifiers).'/';

    // Change the modifier string if needed
    if ( $this->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->modify('Monday this week')->format('Y-m-d');
// And I can still concatenate calls like the DateTime class was intended
echo $test->modify('Monday this week')->modify('+3 days')->format('Y-m-d');

推荐答案

我发现这可行,但 PHP 的 DateTime 类中存在一些不一致之处.

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);

}

}

这篇关于PHP DateTime() 类,将一周的第一天更改为星期一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆