获取一周的第一个/最后一个日期

时间:2022-10-21 10:24:39

Is it possible to get the first / last date of a week using PHP's Relative Date Time format?

是否可以使用PHP的相对日期时间格式获取一周的第一个/最后一个日期?

I've tried to do:

我试过这样做:

date_default_timezone_set('Europe/Amsterdam');
$date = new DateTime();

$date->modify('first day of this week'); // to get the current week's first date
echo $date->format('Y-m-d'); // outputs 2011-12-19

$date->modify('first day of week 50'); // to get the first date of any week by weeknumber
echo $date->format('Y-m-d'); // outputs 2011-12-18

$date->modify('last day of this week'); // to get the current week's last date
echo $date->format('Y-m-d'); // outputs 2011-12-17

$date->modify('last day of week 50'); // to get the last date of any week by weeknumber
echo $date->format('Y-m-d'); // outputs 2011-12-18

As you can see it doesn't output the correct dates.

如您所见,它不会输出正确的日期。

According to the docs this should be possible if I'm correct.

根据文档,如果我是正确的,这应该是可能的。

Am I doing something terrible wrong?

我做错了什么吗?

EDIT

I need to use PHP's DateTime for dates in the far future.

我需要在远期将PHP的DateTime用于日期。

UPDATE

It gets only stranger now. I've done some more testing.

它现在变得更加陌生。我做了一些测试。

Windows PHP 5.3.3

Windows PHP 5.3.3

2011-12-01

Warning: DateTime::modify() [datetime.modify]: Failed to parse time string (first day of week 50) at position 13 (w): The timezone could not be found in the database in C:\Users\Gerrie\Desktop\ph\Websites\Charts\www.charts.com\public\index.php on line 9
2011-12-01
2011-11-30

Warning: DateTime::modify() [datetime.modify]: Failed to parse time string (last day of week 50) at position 12 (w): The timezone could not be found in the database in C:\Users\Gerrie\Desktop\ph\Websites\Charts\www.charts.com\public\index.php on line 15
2011-11-30

Linux 5.3.8

2011-12-01
2011-12-01
2011-11-30
2011-11-30

6 个解决方案

#1


24  

According to docs the format strings "first day of" and "last day of" are only allowed for months, not for weeks. See http://www.php.net/manual/en/datetime.formats.relative.php

根据文档,格式字符串“第一天”和“最后一天”仅允许数月,而不是数周。见http://www.php.net/manual/en/datetime.formats.relative.php

If you combine first and last day of with a week statement the result either blows the parser or is something that you did not expect (usually the first or last day of a month, not a week).

如果将第一天和最后一天与周语句结合使用,结果要么会破坏解析器,要么是您没想到的(通常是一个月的第一天或最后一天,而不是一周)。

The difference that you see between Win and Linux is probably only because of different error reporting settings.

您在Win和Linux之间看到的差异可能仅仅是因为不同的错误报告设置。

To get the first and last day of the current week use:

要获得当周的第一天和最后一天,请使用:

$date->modify('this week');
$date->modify('this week +6 days');

To get the first and last day of week 50 use:

要获得第50周的第一天和最后一天,请使用:

$date->setISODate(2011, 50);
$date->setISODate(2011, 50, 7);

EDIT:

If you want to use the modify method for absolute week numbers you have to use the formats defined in http://www.php.net/manual/en/datetime.formats.compound.php:

如果要对绝对周数使用modify方法,则必须使用http://www.php.net/manual/en/datetime.formats.compound.php中定义的格式:

$date->modify('2011W50');
$date->modify('2011W50 +6 days');

#2


32  

I'm a big fan of using the Carbon library, which makes this sort of thing really easy. For example:

我非常喜欢使用Carbon库,这使得这类事情变得非常简单。例如:

use Carbon\Carbon;

$monday = Carbon::now()->startOfWeek()
$sunday = Carbon::now()->endOfWeek()

Or, if you'd prefer to have Sunday be the first day of your week:

或者,如果您希望将星期日作为您一周的第一天:

use Carbon\Carbon;

Carbon::setWeekStartsAt(Carbon::SUNDAY);
Carbon::setWeekEndsAt(Carbon::SATURDAY);

$sunday = Carbon::now()->startOfWeek()
$saturday = Carbon::now()->endOfWeek()

#3


3  

if first day of week is Monday

如果一周的第一天是星期一

$date->modify('Monday this week');

else if first day is Sunday

否则,如果第一天是星期天

$date->modify('Sunday this week');

because in different countries first day of week maybe Monday or Sunday

因为在不同的国家/地区的第一天可能是周一或周日

#4


2  

This is what I am using to get the first and last day of the week from any date. In this case, monday is the first day of the week...

这是我用来从任何日期获得一周的第一天和最后一天。在这种情况下,星期一是一周的第一天......

$date = date('Y-m-d'); // you can put any date you want
$nbDay = date('N', strtotime($date));
$monday = new DateTime($date);
$sunday = new DateTime($date);
$monday->modify('-'.($nbDay-1).' days');
$sunday->modify('+'.(7-$nbDay).' days');

#5


0  

function getweek_first_last_date($date)
{
    $cur_date = strtotime($date); // Change to whatever date you need
    // Get the day of the week: Sunday = 0 to Saturday = 6
    $dotw = date('w', $cur_date);
    if($dotw>1)
    {
        $pre_monday  =  $cur_date-(($dotw-1)*24*60*60);
        $next_sunday = $cur_date+((7-$dotw)*24*60*60);
    }
    else if($dotw==1)
    {
        $pre_monday  = $cur_date;
        $next_sunday =  $cur_date+((7-$dotw)*24*60*60);
    }
    else if($dotw==0)
    {
        $pre_monday  =$cur_date -(6*24*60*60);;
        $next_sunday = $cur_date;
    }

    $date_array =   array();
    $date_array['start_date_of_week'] = $pre_monday;
    $date_array['end_date_of_week'] = $next_sunday;

    return $date_array;
}

$date = '2013-12-22';
getweek_first_last_date($date);


Output :
$array_of_week = Array
(
    [start_date_of_week] => 1387152000
    [end_date_of_week] => 1387670400
)

$start_date =date('d/m/Y', $array_of_week['start_date_of_week'])

#6


0  

<code>
function getlastweek_first_last_date()
{
   $cur_date = strtotime(date('Y-m-d')); // Change to whatever date you need
    // Get the day of the week: Sunday = 0 to Saturday = 6
    $previousweekcurdate  = $cur_date - (7*24*3600);
    $cur_date = $previousweekcurdate;
    $dotw = date('w', $cur_date);
    if($dotw>1)
    {
        $pre_sunday  =  $cur_date-(($dotw-1)*24*60*60) - (24*60*60);
        $next_satday = $cur_date+((7-$dotw)*24*60*60)- (24*60*60);
    }
    else if($dotw==1)
    {
        $pre_sunday  = $cur_date- (24*60*60);
        $next_satday =  $cur_date+((7-$dotw)*24*60*60)- (24*60*60);
    }
    else if($dotw==0)
    {
        $pre_sunday  =$cur_date -(6*24*60*60)- (24*60*60);
        $next_satday = $cur_date- (24*60*60);
    }

    $pre_sunday = date('Y-m-d',$pre_sunday)." 00:00:00";
    $next_satday = date('Y-m-d',$next_satday)." 23:59:59";
    $date_array =   array();
    $date_array['sdoflw'] = $pre_sunday;
    $date_array['edoflw'] = $next_satday;

    return $date_array;
}

$date_array = getlastweek_first_last_date();
echo $start_date_of_week = $date_array['sdoflw'];
echo $end_date_of_week = $date_array['edoflw'];

</code> 

#1


24  

According to docs the format strings "first day of" and "last day of" are only allowed for months, not for weeks. See http://www.php.net/manual/en/datetime.formats.relative.php

根据文档,格式字符串“第一天”和“最后一天”仅允许数月,而不是数周。见http://www.php.net/manual/en/datetime.formats.relative.php

If you combine first and last day of with a week statement the result either blows the parser or is something that you did not expect (usually the first or last day of a month, not a week).

如果将第一天和最后一天与周语句结合使用,结果要么会破坏解析器,要么是您没想到的(通常是一个月的第一天或最后一天,而不是一周)。

The difference that you see between Win and Linux is probably only because of different error reporting settings.

您在Win和Linux之间看到的差异可能仅仅是因为不同的错误报告设置。

To get the first and last day of the current week use:

要获得当周的第一天和最后一天,请使用:

$date->modify('this week');
$date->modify('this week +6 days');

To get the first and last day of week 50 use:

要获得第50周的第一天和最后一天,请使用:

$date->setISODate(2011, 50);
$date->setISODate(2011, 50, 7);

EDIT:

If you want to use the modify method for absolute week numbers you have to use the formats defined in http://www.php.net/manual/en/datetime.formats.compound.php:

如果要对绝对周数使用modify方法,则必须使用http://www.php.net/manual/en/datetime.formats.compound.php中定义的格式:

$date->modify('2011W50');
$date->modify('2011W50 +6 days');

#2


32  

I'm a big fan of using the Carbon library, which makes this sort of thing really easy. For example:

我非常喜欢使用Carbon库,这使得这类事情变得非常简单。例如:

use Carbon\Carbon;

$monday = Carbon::now()->startOfWeek()
$sunday = Carbon::now()->endOfWeek()

Or, if you'd prefer to have Sunday be the first day of your week:

或者,如果您希望将星期日作为您一周的第一天:

use Carbon\Carbon;

Carbon::setWeekStartsAt(Carbon::SUNDAY);
Carbon::setWeekEndsAt(Carbon::SATURDAY);

$sunday = Carbon::now()->startOfWeek()
$saturday = Carbon::now()->endOfWeek()

#3


3  

if first day of week is Monday

如果一周的第一天是星期一

$date->modify('Monday this week');

else if first day is Sunday

否则,如果第一天是星期天

$date->modify('Sunday this week');

because in different countries first day of week maybe Monday or Sunday

因为在不同的国家/地区的第一天可能是周一或周日

#4


2  

This is what I am using to get the first and last day of the week from any date. In this case, monday is the first day of the week...

这是我用来从任何日期获得一周的第一天和最后一天。在这种情况下,星期一是一周的第一天......

$date = date('Y-m-d'); // you can put any date you want
$nbDay = date('N', strtotime($date));
$monday = new DateTime($date);
$sunday = new DateTime($date);
$monday->modify('-'.($nbDay-1).' days');
$sunday->modify('+'.(7-$nbDay).' days');

#5


0  

function getweek_first_last_date($date)
{
    $cur_date = strtotime($date); // Change to whatever date you need
    // Get the day of the week: Sunday = 0 to Saturday = 6
    $dotw = date('w', $cur_date);
    if($dotw>1)
    {
        $pre_monday  =  $cur_date-(($dotw-1)*24*60*60);
        $next_sunday = $cur_date+((7-$dotw)*24*60*60);
    }
    else if($dotw==1)
    {
        $pre_monday  = $cur_date;
        $next_sunday =  $cur_date+((7-$dotw)*24*60*60);
    }
    else if($dotw==0)
    {
        $pre_monday  =$cur_date -(6*24*60*60);;
        $next_sunday = $cur_date;
    }

    $date_array =   array();
    $date_array['start_date_of_week'] = $pre_monday;
    $date_array['end_date_of_week'] = $next_sunday;

    return $date_array;
}

$date = '2013-12-22';
getweek_first_last_date($date);


Output :
$array_of_week = Array
(
    [start_date_of_week] => 1387152000
    [end_date_of_week] => 1387670400
)

$start_date =date('d/m/Y', $array_of_week['start_date_of_week'])

#6


0  

<code>
function getlastweek_first_last_date()
{
   $cur_date = strtotime(date('Y-m-d')); // Change to whatever date you need
    // Get the day of the week: Sunday = 0 to Saturday = 6
    $previousweekcurdate  = $cur_date - (7*24*3600);
    $cur_date = $previousweekcurdate;
    $dotw = date('w', $cur_date);
    if($dotw>1)
    {
        $pre_sunday  =  $cur_date-(($dotw-1)*24*60*60) - (24*60*60);
        $next_satday = $cur_date+((7-$dotw)*24*60*60)- (24*60*60);
    }
    else if($dotw==1)
    {
        $pre_sunday  = $cur_date- (24*60*60);
        $next_satday =  $cur_date+((7-$dotw)*24*60*60)- (24*60*60);
    }
    else if($dotw==0)
    {
        $pre_sunday  =$cur_date -(6*24*60*60)- (24*60*60);
        $next_satday = $cur_date- (24*60*60);
    }

    $pre_sunday = date('Y-m-d',$pre_sunday)." 00:00:00";
    $next_satday = date('Y-m-d',$next_satday)." 23:59:59";
    $date_array =   array();
    $date_array['sdoflw'] = $pre_sunday;
    $date_array['edoflw'] = $next_satday;

    return $date_array;
}

$date_array = getlastweek_first_last_date();
echo $start_date_of_week = $date_array['sdoflw'];
echo $end_date_of_week = $date_array['edoflw'];

</code>