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

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

问题描述

PHP(5.3+)中的DateTime类工作得很好,只要您所在国家/地区的第一天是星期天。在荷兰,本周的第一天是星期一,只是使这个类无法建立一个周视图和计算的日历。



我似乎找不到在Stackoverflow或其余Internet上的答案如何让DateTime的行为好像一周的第一天是星期一。



我在Stackoverflow上发现了这个片段,但是它不会解决所有的方法,你可以陷入麻烦,这不是一个优雅的解决方案。

  $ dateTime = new DateTime '2012-05-14'); 
$ monday = clone $ dateTime-> modify(('Sunday'== $ dateTime-> format('l'))?'星期一上周':'星期一'

有办法改变这个或扩展日期时间?无法想象这不是一个设置,因为大多数欧洲的星期一开始他们的星期。



添加:
发布完整的日历和函数代码不会使事情更清楚。但是这里是一行为例。
我经常需要检查一周的第一天是什么或计算从一周的第一天到该周的不同的日期和时间。我的代码充满了这些:

  $ startOfWeek = $ date-> modify(('Sunday'== $ date - > format('l'))?'上周星期一':'本周星期') - > modify('+ 3小时') - >格式(DATETIME); 

我也得到一个不想要的结果,试图获得一个月或一年的第一个完整周。因为我的$ date对象不总是包含相同的日期,我不得不继续检查它这样,使代码难以阅读。



编辑虽然有一些不一致的地方, 。对于一些奇怪的原因DateTime确实得到下一个正确:

  $ test = new DateTime('2012-10-29') ; // Monday 
echo $ test-> modify('Sunday this week') - > format('Y-m-d'); // 2012-11-04

//但...

$ test = new DateTime('2012-11-04'); // Sunday
echo $ test-> modify('Monday this week') - > format('Y-m-d'); // 2012-11-05而不是2012-10-29

但我想我可以问题更清楚:
DateTime()类可以用星期一作为一周的第一天。

更新:
好吧,我想我得到某个地方...我不是一个职业编程类。但这似乎工作了几个星期。但它仍然需要规则第一天,第二天...也为星期天名称本身。我不认为这是万无一失的。

  class EuroDateTime extends DateTime {

// Fields
private $ weekModifiers = array(
'this week',
'next week',
'上周',
'上周'
);

//覆盖modify()
public function modify($ string){

//搜索模式
$ pattern ='/' .implode('|',$ this-> weekModifiers)。'/';

//如果需要,更改修饰符字符串
if($ this-> format('N')== 7){//这是星期日
$ matches = array ();
if(preg_match($ pattern,$ string,$ matches)){
$ string = str_replace($ matches [0],'-7 days'。
}
}
return parent :: modify($ string);

}

}
//这个工作
$ test = new EuroDateTime('2012-11-04');
echo $ test-> modify('Monday this week') - > format('Y-m-d');
//我仍然可以连接类似DateTime类的调用
echo $ test-> modify('Monday this week') - > modify('+ 3 days') - > format('Ym-d');


解决方案

我发现这个工作,但有一些不一致在PHP的DateTime类中。



如果出发日期是星期日,则上一个星期不被视为同一周(由此类固定)。但是从星期一离开,下星期天被认为是同一周。如果他们在将来修复这个类将需要一些添加。

  class EuroDateTime extends DateTime {

//覆盖modify()
public function modify($ string){

//如果需要,更改修饰符字符串
if($ this-> format 'N')== 7){//这是星期天,我们计算一个使用相对周数的日子
$ matches = array();
$ pattern ='/本周|下周|上周|上周/ i';
if(preg_match($ pattern,$ string,$ matches)){
$ string = str_replace($ matches [0],'-7 days'。
}
}
return parent :: modify($ string);

}

}


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

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

解决方案

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天全站免登陆