在PHP中获得第一天的时间?

时间:2021-11-29 07:24:45

Given a date MM-dd-yyyy format, can someone help me get the first day of the week?

给定一个日期mdd -yyyy格式,有人能帮助我获得一周的第一天吗?

34 个解决方案

#1


105  

Here is what I am using...

这是我正在使用的…

$day = date('w');
$week_start = date('m-d-Y', strtotime('-'.$day.' days'));
$week_end = date('m-d-Y', strtotime('+'.(6-$day).' days'));

$day contains a number from 0 to 6 representing the day of the week (Sunday = 0, Monday = 1, etc.).
$week_start contains the date for Sunday of the current week as mm-dd-yyyy.
$week_end contains the date for the Saturday of the current week as mm-dd-yyyy.

$day包含一个数字,从0到6表示一周的天数(星期日= 0,星期一= 1,等等)。$week_start包含了本星期的周日作为mdd -yyyy的日期。$week_end包含了当前星期的星期六的日期,作为mdd -yyyy。

#2


56  

strtotime('this week', time());

Replace time(). Next sunday/last monday methods won't work when the current day is sunday/monday.

更换时间()。下星期天/上星期一的方法在今天是星期天/星期一的时候是行不通的。

#3


44  

Very simple to use strtotime function:

使用strtotime函数非常简单:

echo date("Y-m-d", strtotime('monday this week')), "\n";   

echo date("Y-m-d", strtotime('sunday this week')), "\n";

It differs a bit across PHP versions:

它在PHP版本中略有不同:

Output for 5.3.0 - 5.6.6, php7@20140507 - 20150301, hhvm-3.3.1 - 3.5.1

2015-03-16
2015-03-22

Output for 4.3.5 - 5.2.17

2015-03-23
2015-03-22

Output for 4.3.0 - 4.3.4

2015-03-30
2015-03-29

Comparing at Edge-Cases

Relative descriptions like this week have their own context. The following shows the output for this week monday and sunday when it's a monday or a sunday:

这周的相关描述有他们自己的背景。以下是周一或周日的周一或周日的产出:

$date = '2015-03-16'; // monday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";   
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";

$date = '2015-03-22'; // sunday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";   
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";

Againt it differs a bit across PHP versions:

不同的PHP版本有一点不同:

Output for 5.3.0 - 5.6.6, php7@20140507 - 20150301, hhvm-3.3.1 - 3.5.1

2015-03-16
2015-03-22
2015-03-23
2015-03-29

Output for 4.3.5 - 5.0.5, 5.2.0 - 5.2.17

2015-03-16
2015-03-22
2015-03-23
2015-03-22

Output for 5.1.0 - 5.1.6

2015-03-23
2015-03-22
2015-03-23
2015-03-29

Output for 4.3.0 - 4.3.4

2015-03-23
2015-03-29
2015-03-30
2015-03-29

#4


36  

Keep it simple :

保持简单:

<?php    
$dateTime = new \DateTime('2012-05-14');
$monday = clone $dateTime->modify(('Sunday' == $dateTime->format('l')) ? 'Monday last week' : 'Monday this week');
$sunday = clone $dateTime->modify('Sunday this week');    
?>

Source : PHP manual

来源:PHP手册

#5


22  

$givenday = date("w", mktime(0, 0, 0, MM, dd, yyyy));

This gives you the day of the week of the given date itself where 0 = Sunday and 6 = Saturday. From there you can simply calculate backwards to the day you want.

这就给出了给定日期的日期,其中0 = Sunday和6 =周六。从那里你可以简单地计算出你想要的一天。

#6


14  

For what it's worth, here is a breakdown of the wonky behavior of strtotime when determining a consistent frame of reference:

对于它的价值,这里列出了在确定一个一致的参照系时,strtotime的古怪行为的分类:

http://gamereplays.org/reference/strtotime.php

http://gamereplays.org/reference/strtotime.php

Basically only these strings will reliably give you the same date, no matter what day of the week you're currently on when you call them:

基本上,只有这些字符串会可靠地给你相同的日期,不管你现在是什么时候打电话给他们:

strtotime("next monday");
strtotime("this sunday");
strtotime("last sunday"); 

#7


13  

This question needs a good DateTime answer:-

这个问题需要一个好的日期时间答案:-。

function firstDayOfWeek($date)
{
    $day = DateTime::createFromFormat('m-d-Y', $date);
    $day->setISODate((int)$day->format('o'), (int)$day->format('W'), 1);
    return $day->format('m-d-Y');
}

var_dump(firstDayOfWeek('06-13-2013'));

Output:-

输出:

string '06-10-2013' (length=10)

This will deal with year boundaries and leap years.

这将涉及到年界限和闰年。

#8


11  

The following code should work with any custom date, just uses the desired date format.

下面的代码应该与任何自定义日期一起工作,只是使用了所需的日期格式。

$custom_date = strtotime( date('d-m-Y', strtotime('31-07-2012')) ); 
$week_start = date('d-m-Y', strtotime('this week last monday', $custom_date));
$week_end = date('d-m-Y', strtotime('this week next sunday', $custom_date));
echo '<br>Start: '. $week_start;
echo '<br>End: '. $week_end;

I tested the code with PHP 5.2.17 Results:

我用PHP 5.2.17测试了代码:

Start: 30-07-2012
End: 05-08-2012

#9


8  

How about this?

这个怎么样?

$first_day_of_week = date('m-d-Y', strtotime('Last Monday', time()));
$last_day_of_week = date('m-d-Y', strtotime('Next Sunday', time()));

#10


7  

Assuming Monday as the first day of the week, this works:

假设周一是一周的第一天,这是可行的:

echo date("M-d-y", strtotime('last monday', strtotime('next week', time())));

#11


6  

Here I am considering Sunday as first & Saturday as last day of the week.

在这里,我把星期日作为第一和星期六作为一周的最后一天。

$m = strtotime('06-08-2012');  
$today =   date('l', $m);  
$custom_date = strtotime( date('d-m-Y', $m) );   
if ($today == 'Sunday') {  
   $week_start = date("d-m-Y", $m);  
} else {  
  $week_start = date('d-m-Y', strtotime('this week last sunday', $custom_date));  
}  

if ($today == 'Saturday') {  
  $week_end = date("d-m-Y", $m);
} else {  
  $week_end = date('d-m-Y', strtotime('this week next saturday', $custom_date));  
}
echo '<br>Start: '. $week_start;  
echo '<br>End: '. $week_end;  

Output :

输出:

Start: 05-08-2012
End: 11-08-2012

结束开始:05-08-2012:05-08-2012

#12


4  

How about this?

这个怎么样?

$day_of_week = date('N', strtotime($string_date));
$week_first_day = date('Y-m-d', strtotime($string_date . " - " . ($day_of_week - 1) . " days"));
$week_last_day = date('Y-m-d', strtotime($string_date . " + " . (7 - $day_of_week) . " days"));

#13


4  

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

#14


3  

Try this:

试试这个:

function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
    $wk_ts  = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
    $mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
    return date($format, $mon_ts);
}

$sStartDate = week_start_date($week_number, $year);
$sEndDate   = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));

(from this forum thread)

(从这个论坛线程)

#15


3  

This is the shortest and most readable solution I found:

这是我找到的最短、最易读的解决方案:

    <?php
    $weekstart = strtotime('monday this week');
    $weekstop = strtotime('sunday this week 23:59:59');
    //echo date('d.m.Y H:i:s', $weekstart) .' - '. date('d.m.Y H:i:s', $weekstop);
    ?>

strtotime is faster than new DateTime()->getTimestamp().

strtotime比新DateTime()->getTimestamp()快。

#16


3  

Just use date($format, strtotime($date,' LAST SUNDAY + 1 DAY'));

使用日期($format, strtotime($date, LAST SUNDAY + 1 DAY));

#17


2  

Given PHP version pre 5.3 following function gives you a first day of the week of given date (in this case - Sunday, 2013-02-03):

给定PHP版本前5.3的函数给您提供了给定日期的第一天(在本例中为2013-02-03):

<?php
  function startOfWeek($aDate){
    $d=strtotime($aDate);
    return strtotime(date('Y-m-d',$d).' - '.date("w",$d).' days');
  }

  echo(date('Y-m-d',startOfWeek("2013-02-07")).'
');
?>

#18


2  

$today_day = date('D'); //Or add your own date
$start_of_week = date('Ymd');
$end_of_week = date('Ymd');

if($today_day != "Mon")
    $start_of_week = date('Ymd', strtotime("last monday"));

if($today_day != "Sun")
                    $end_of_week = date('Ymd', strtotime("next sunday"));

#19


2  

$monday = date('d-m-Y',strtotime('last monday',strtotime('next monday',strtotime($date))));

You have to get next monday first then get the 'last monday' of next monday. So if the given date is monday it will return the same date not last week monday.

你必须在下周一先去,然后在下周一的最后一个星期一。所以,如果给定的日期是星期一,它将返回相同的日期,而不是上星期一。

#20


2  

If you want Monday as the start of your week, do this:

如果你想让周一成为你一周的开始,那么你可以这样做:

$date = '2015-10-12';
$day = date('N', strtotime($date));
$week_start = date('Y-m-d', strtotime('-'.($day-1).' days', strtotime($date)));
$week_end = date('Y-m-d', strtotime('+'.(7-$day).' days', strtotime($date)));

#21


1  

You parse the date using strptime() and use date() on the result:

您可以使用strptime()和使用date()来解析结果:

date('N', strptime('%m-%d-%g', $dateString));

#22


1  

<?php
/* PHP 5.3.0 */

date_default_timezone_set('America/Denver'); //Set apprpriate timezone
$start_date = strtotime('2009-12-15'); //Set start date

//Today's date if $start_date is a Sunday, otherwise date of previous Sunday
$today_or_previous_sunday = mktime(0, 0, 0, date('m', $start_date), date('d', $start_date), date('Y', $start_date)) - ((date("w", $start_date) ==0) ? 0 : (86400 * date("w", $start_date)));

//prints 12-13-2009 (month-day-year)
echo date('m-d-Y', $today_or_previous_sunday);

?>

(Note: MM, dd and yyyy in the Question are not standard php date format syntax - I can't be sure what is meant, so I set the $start_date with ISO year-month-day)

(注意:问题中的MM、dd和yyyy不是标准的php日期格式语法——我不确定是什么意思,所以我将$start_date设置为ISO年月日)

#23


1  

Another way to do it....

另一种方式去做....

$year = '2014';
$month = '02';
$day = '26';

$date = DateTime::createFromFormat('Y-m-d H:i:s', $year . '-' . $month . '-' . $day . '00:00:00');
$day = date('w', $date->getTimestamp());

// 0=Sunday 6=Saturday
if($day!=0){

   $newdate = $date->getTimestamp() - $day * 86400;  //86400 seconds in a day

   // Look for DST change 
   if($old = date('I', $date->getTimestamp()) != $new = date('I', $newdate)){
       if($old == 0){
           $newdate -= 3600;  //3600 seconds in an hour
       } else {
           $newdate += 3600;
       }
   }

   $date->setTimestamp($newdate);
}

echo $date->format('D Y-m-d H:i:s');

#24


1  

The easiest way to get first day(Monday) of current week is:

第一天(星期一)最简单的方法是:

strtotime("next Monday") - 604800;

where 604800 - is count of seconds in 1 week(60*60*24*7).

604800 -在1周内秒数(60*60*24*7)。

This code get next Monday and decrease it for 1 week. This code will work well in any day of week. Even if today is Monday.

这段代码在下周一,并减少1周。这段代码在一周的任何一天都很有效。即使今天是星期一。

#25


1  

I found this quite frustrating given that my timezone is Australian and that strtotime() hates UK dates.

我觉得这很令人沮丧,因为我的时区是澳大利亚的,strtotime()讨厌英国的日期。

If the current day is a Sunday, then strtotime("monday this week") will return the day after.

如果现在是周日,那么strtotime(“本周星期一”)将会在第二天返回。

To overcome this:

为了克服这个:

Caution: This is only valid for Australian/UK dates

警告:这只适用于澳大利亚/英国的日期。

$startOfWeek = (date('l') == 'Monday') ? date('d/m/Y 00:00') : date('d/m/Y', strtotime("last monday 00:00"));
$endOfWeek = (date('l') == 'Sunday') ? date('d/m/Y 23:59:59') : date('d/m/Y', strtotime("sunday 23:59:59"));

#26


1  

A smart way of doing this is to let PHP handle timezone differences and Daylight Savings Time (DST). Let me show you how to do this.

一种巧妙的方法是让PHP处理时区差异和夏令时(DST)。让我告诉你怎么做。

This function will generate all days from Monday until Friday, inclusive (handy for generating work week days):

该功能将从周一到周五全天提供,包括(便于产生工作日):

class DateTimeUtilities {
    public static function getPeriodFromMondayUntilFriday($offset = 'now') {
        $now = new \DateTimeImmutable($offset, new \DateTimeZone('UTC'));
        $today = $now->setTime(0, 0, 1);

        $daysFromMonday = $today->format('N') - 1;

        $monday = $today->sub(new \DateInterval(sprintf('P%dD', $daysFromMonday)));
        $saturday = $monday->add(new \DateInterval('P5D'));

        return new \DatePeriod($monday, new \DateInterval('P1D'), $saturday);
    }
}

foreach (DateTimeUtilities::getPeriodFromMondayUntilFriday() as $day) {
    print $day->format('c');
    print PHP_EOL;
}

This will return datetimes Monday-Friday for current week. To do the same for an arbitrary date, pass a date as a parameter to DateTimeUtilities ::getPeriodFromMondayUntilFriday, thus:

这将会在周一返回datetimes周一到周五。对于任意日期执行相同的操作,将日期作为参数传递给DateTimeUtilities::getPeriodFromMondayUntilFriday,因此:

foreach (DateTimeUtilities::getPeriodFromMondayUntilFriday('2017-01-02T15:05:21+00:00') as $day) {
    print $day->format('c');
    print PHP_EOL;
}

//prints 
//2017-01-02T00:00:01+00:00
//2017-01-03T00:00:01+00:00
//2017-01-04T00:00:01+00:00
//2017-01-05T00:00:01+00:00
//2017-01-06T00:00:01+00:00

Only interested in Monday, as the OP asked?

只对周一感兴趣,就像OP问的那样?

$monday = DateTimeUtilities::getPeriodFromMondayUntilFriday('2017-01-02T15:05:21+00:00')->getStartDate()->format('c');
print $monday;
// prints
//2017-01-02T00:00:01+00:00

#27


0  

I've come against this question a few times and always surprised the date functions don't make this easier or clearer. Here's my solution for PHP5 that uses the DateTime class:

我已经对这个问题做了几次反驳,并且总是惊讶于日期函数并没有使它变得更简单或更清晰。这里是我的PHP5的解决方案,它使用DateTime类:

/**
 * @param DateTime $date A given date
 * @param int $firstDay 0-6, Sun-Sat respectively
 * @return DateTime
 */
function getFirstDayOfWeek(DateTime $date, $firstDay = 0) {
    $offset = 7 - $firstDay;
    $ret = clone $date;
    $ret->modify(-(($date->format('w') + $offset) % 7) . 'days');
    return $ret;
}

Necessary to clone to avoid altering the original date.

需要克隆以避免更改原始日期。

#28


0  

What about:

是什么:

$date = "2013-03-18"; 
$searchRow = date("d.m.Y", strtotime('last monday',strtotime($date." + 1 day")));
echo "for date " .$date ." we get this monday: " ;echo $searchRow; echo '<br>';

Its not the best way but i tested and if i am in this week i get the correct monday, and if i am on a monday i will get that monday.

这不是最好的方法,但我测试过,如果我在这周,我得到了正确的星期一,如果我在星期一,我会得到那个星期一。

#29


0  

I found this solution helpful. Just subtract if it isn't monday to get the previous Monday. I am using $lower_date as the date I pulled from a query that I then need to reconcile to the previous Monday.

我发现这个解决方案很有帮助。如果周一不是周一就做减法。我使用$lower_date作为我从查询中提取的日期,然后需要与前一个星期一进行协调。

//set this up to go backwards until you find monday
while(date('D',strtotime($lower_date))!='Mon'){
    $lower_date = date('Y-m-d', strtotime($lower_date . ' - 1 day')); //increase the upper spec
}

#30


0  

this one is prepared for today is monday

今天是星期一。

function lastMonday(\DateTime $date) {

    $timestamp = $date->getTimestamp();

    $monday = ( 1 == date( 'N', $timestamp ));

    if ($monday) {
        return $timestamp;
    } else {
        return strtotime( 'last monday', $timestamp );
    }
}

if you want it to get timestamp instead of DateTime change first two lines (get rid of date->getTimestamp) change them to just this

如果您希望它获得时间戳而不是DateTime更改前两行(删除日期->getTimestamp),将它们更改为这样。

function lastMonday($timestamp) {

and if you want it to input string change first two lines to this

如果你想让它输入字符串改变前两行。

function lastMonday($dateString) {

    $timestamp = strtotime($dateString);

#1


105  

Here is what I am using...

这是我正在使用的…

$day = date('w');
$week_start = date('m-d-Y', strtotime('-'.$day.' days'));
$week_end = date('m-d-Y', strtotime('+'.(6-$day).' days'));

$day contains a number from 0 to 6 representing the day of the week (Sunday = 0, Monday = 1, etc.).
$week_start contains the date for Sunday of the current week as mm-dd-yyyy.
$week_end contains the date for the Saturday of the current week as mm-dd-yyyy.

$day包含一个数字,从0到6表示一周的天数(星期日= 0,星期一= 1,等等)。$week_start包含了本星期的周日作为mdd -yyyy的日期。$week_end包含了当前星期的星期六的日期,作为mdd -yyyy。

#2


56  

strtotime('this week', time());

Replace time(). Next sunday/last monday methods won't work when the current day is sunday/monday.

更换时间()。下星期天/上星期一的方法在今天是星期天/星期一的时候是行不通的。

#3


44  

Very simple to use strtotime function:

使用strtotime函数非常简单:

echo date("Y-m-d", strtotime('monday this week')), "\n";   

echo date("Y-m-d", strtotime('sunday this week')), "\n";

It differs a bit across PHP versions:

它在PHP版本中略有不同:

Output for 5.3.0 - 5.6.6, php7@20140507 - 20150301, hhvm-3.3.1 - 3.5.1

2015-03-16
2015-03-22

Output for 4.3.5 - 5.2.17

2015-03-23
2015-03-22

Output for 4.3.0 - 4.3.4

2015-03-30
2015-03-29

Comparing at Edge-Cases

Relative descriptions like this week have their own context. The following shows the output for this week monday and sunday when it's a monday or a sunday:

这周的相关描述有他们自己的背景。以下是周一或周日的周一或周日的产出:

$date = '2015-03-16'; // monday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";   
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";

$date = '2015-03-22'; // sunday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";   
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";

Againt it differs a bit across PHP versions:

不同的PHP版本有一点不同:

Output for 5.3.0 - 5.6.6, php7@20140507 - 20150301, hhvm-3.3.1 - 3.5.1

2015-03-16
2015-03-22
2015-03-23
2015-03-29

Output for 4.3.5 - 5.0.5, 5.2.0 - 5.2.17

2015-03-16
2015-03-22
2015-03-23
2015-03-22

Output for 5.1.0 - 5.1.6

2015-03-23
2015-03-22
2015-03-23
2015-03-29

Output for 4.3.0 - 4.3.4

2015-03-23
2015-03-29
2015-03-30
2015-03-29

#4


36  

Keep it simple :

保持简单:

<?php    
$dateTime = new \DateTime('2012-05-14');
$monday = clone $dateTime->modify(('Sunday' == $dateTime->format('l')) ? 'Monday last week' : 'Monday this week');
$sunday = clone $dateTime->modify('Sunday this week');    
?>

Source : PHP manual

来源:PHP手册

#5


22  

$givenday = date("w", mktime(0, 0, 0, MM, dd, yyyy));

This gives you the day of the week of the given date itself where 0 = Sunday and 6 = Saturday. From there you can simply calculate backwards to the day you want.

这就给出了给定日期的日期,其中0 = Sunday和6 =周六。从那里你可以简单地计算出你想要的一天。

#6


14  

For what it's worth, here is a breakdown of the wonky behavior of strtotime when determining a consistent frame of reference:

对于它的价值,这里列出了在确定一个一致的参照系时,strtotime的古怪行为的分类:

http://gamereplays.org/reference/strtotime.php

http://gamereplays.org/reference/strtotime.php

Basically only these strings will reliably give you the same date, no matter what day of the week you're currently on when you call them:

基本上,只有这些字符串会可靠地给你相同的日期,不管你现在是什么时候打电话给他们:

strtotime("next monday");
strtotime("this sunday");
strtotime("last sunday"); 

#7


13  

This question needs a good DateTime answer:-

这个问题需要一个好的日期时间答案:-。

function firstDayOfWeek($date)
{
    $day = DateTime::createFromFormat('m-d-Y', $date);
    $day->setISODate((int)$day->format('o'), (int)$day->format('W'), 1);
    return $day->format('m-d-Y');
}

var_dump(firstDayOfWeek('06-13-2013'));

Output:-

输出:

string '06-10-2013' (length=10)

This will deal with year boundaries and leap years.

这将涉及到年界限和闰年。

#8


11  

The following code should work with any custom date, just uses the desired date format.

下面的代码应该与任何自定义日期一起工作,只是使用了所需的日期格式。

$custom_date = strtotime( date('d-m-Y', strtotime('31-07-2012')) ); 
$week_start = date('d-m-Y', strtotime('this week last monday', $custom_date));
$week_end = date('d-m-Y', strtotime('this week next sunday', $custom_date));
echo '<br>Start: '. $week_start;
echo '<br>End: '. $week_end;

I tested the code with PHP 5.2.17 Results:

我用PHP 5.2.17测试了代码:

Start: 30-07-2012
End: 05-08-2012

#9


8  

How about this?

这个怎么样?

$first_day_of_week = date('m-d-Y', strtotime('Last Monday', time()));
$last_day_of_week = date('m-d-Y', strtotime('Next Sunday', time()));

#10


7  

Assuming Monday as the first day of the week, this works:

假设周一是一周的第一天,这是可行的:

echo date("M-d-y", strtotime('last monday', strtotime('next week', time())));

#11


6  

Here I am considering Sunday as first & Saturday as last day of the week.

在这里,我把星期日作为第一和星期六作为一周的最后一天。

$m = strtotime('06-08-2012');  
$today =   date('l', $m);  
$custom_date = strtotime( date('d-m-Y', $m) );   
if ($today == 'Sunday') {  
   $week_start = date("d-m-Y", $m);  
} else {  
  $week_start = date('d-m-Y', strtotime('this week last sunday', $custom_date));  
}  

if ($today == 'Saturday') {  
  $week_end = date("d-m-Y", $m);
} else {  
  $week_end = date('d-m-Y', strtotime('this week next saturday', $custom_date));  
}
echo '<br>Start: '. $week_start;  
echo '<br>End: '. $week_end;  

Output :

输出:

Start: 05-08-2012
End: 11-08-2012

结束开始:05-08-2012:05-08-2012

#12


4  

How about this?

这个怎么样?

$day_of_week = date('N', strtotime($string_date));
$week_first_day = date('Y-m-d', strtotime($string_date . " - " . ($day_of_week - 1) . " days"));
$week_last_day = date('Y-m-d', strtotime($string_date . " + " . (7 - $day_of_week) . " days"));

#13


4  

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

#14


3  

Try this:

试试这个:

function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
    $wk_ts  = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
    $mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
    return date($format, $mon_ts);
}

$sStartDate = week_start_date($week_number, $year);
$sEndDate   = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));

(from this forum thread)

(从这个论坛线程)

#15


3  

This is the shortest and most readable solution I found:

这是我找到的最短、最易读的解决方案:

    <?php
    $weekstart = strtotime('monday this week');
    $weekstop = strtotime('sunday this week 23:59:59');
    //echo date('d.m.Y H:i:s', $weekstart) .' - '. date('d.m.Y H:i:s', $weekstop);
    ?>

strtotime is faster than new DateTime()->getTimestamp().

strtotime比新DateTime()->getTimestamp()快。

#16


3  

Just use date($format, strtotime($date,' LAST SUNDAY + 1 DAY'));

使用日期($format, strtotime($date, LAST SUNDAY + 1 DAY));

#17


2  

Given PHP version pre 5.3 following function gives you a first day of the week of given date (in this case - Sunday, 2013-02-03):

给定PHP版本前5.3的函数给您提供了给定日期的第一天(在本例中为2013-02-03):

<?php
  function startOfWeek($aDate){
    $d=strtotime($aDate);
    return strtotime(date('Y-m-d',$d).' - '.date("w",$d).' days');
  }

  echo(date('Y-m-d',startOfWeek("2013-02-07")).'
');
?>

#18


2  

$today_day = date('D'); //Or add your own date
$start_of_week = date('Ymd');
$end_of_week = date('Ymd');

if($today_day != "Mon")
    $start_of_week = date('Ymd', strtotime("last monday"));

if($today_day != "Sun")
                    $end_of_week = date('Ymd', strtotime("next sunday"));

#19


2  

$monday = date('d-m-Y',strtotime('last monday',strtotime('next monday',strtotime($date))));

You have to get next monday first then get the 'last monday' of next monday. So if the given date is monday it will return the same date not last week monday.

你必须在下周一先去,然后在下周一的最后一个星期一。所以,如果给定的日期是星期一,它将返回相同的日期,而不是上星期一。

#20


2  

If you want Monday as the start of your week, do this:

如果你想让周一成为你一周的开始,那么你可以这样做:

$date = '2015-10-12';
$day = date('N', strtotime($date));
$week_start = date('Y-m-d', strtotime('-'.($day-1).' days', strtotime($date)));
$week_end = date('Y-m-d', strtotime('+'.(7-$day).' days', strtotime($date)));

#21


1  

You parse the date using strptime() and use date() on the result:

您可以使用strptime()和使用date()来解析结果:

date('N', strptime('%m-%d-%g', $dateString));

#22


1  

<?php
/* PHP 5.3.0 */

date_default_timezone_set('America/Denver'); //Set apprpriate timezone
$start_date = strtotime('2009-12-15'); //Set start date

//Today's date if $start_date is a Sunday, otherwise date of previous Sunday
$today_or_previous_sunday = mktime(0, 0, 0, date('m', $start_date), date('d', $start_date), date('Y', $start_date)) - ((date("w", $start_date) ==0) ? 0 : (86400 * date("w", $start_date)));

//prints 12-13-2009 (month-day-year)
echo date('m-d-Y', $today_or_previous_sunday);

?>

(Note: MM, dd and yyyy in the Question are not standard php date format syntax - I can't be sure what is meant, so I set the $start_date with ISO year-month-day)

(注意:问题中的MM、dd和yyyy不是标准的php日期格式语法——我不确定是什么意思,所以我将$start_date设置为ISO年月日)

#23


1  

Another way to do it....

另一种方式去做....

$year = '2014';
$month = '02';
$day = '26';

$date = DateTime::createFromFormat('Y-m-d H:i:s', $year . '-' . $month . '-' . $day . '00:00:00');
$day = date('w', $date->getTimestamp());

// 0=Sunday 6=Saturday
if($day!=0){

   $newdate = $date->getTimestamp() - $day * 86400;  //86400 seconds in a day

   // Look for DST change 
   if($old = date('I', $date->getTimestamp()) != $new = date('I', $newdate)){
       if($old == 0){
           $newdate -= 3600;  //3600 seconds in an hour
       } else {
           $newdate += 3600;
       }
   }

   $date->setTimestamp($newdate);
}

echo $date->format('D Y-m-d H:i:s');

#24


1  

The easiest way to get first day(Monday) of current week is:

第一天(星期一)最简单的方法是:

strtotime("next Monday") - 604800;

where 604800 - is count of seconds in 1 week(60*60*24*7).

604800 -在1周内秒数(60*60*24*7)。

This code get next Monday and decrease it for 1 week. This code will work well in any day of week. Even if today is Monday.

这段代码在下周一,并减少1周。这段代码在一周的任何一天都很有效。即使今天是星期一。

#25


1  

I found this quite frustrating given that my timezone is Australian and that strtotime() hates UK dates.

我觉得这很令人沮丧,因为我的时区是澳大利亚的,strtotime()讨厌英国的日期。

If the current day is a Sunday, then strtotime("monday this week") will return the day after.

如果现在是周日,那么strtotime(“本周星期一”)将会在第二天返回。

To overcome this:

为了克服这个:

Caution: This is only valid for Australian/UK dates

警告:这只适用于澳大利亚/英国的日期。

$startOfWeek = (date('l') == 'Monday') ? date('d/m/Y 00:00') : date('d/m/Y', strtotime("last monday 00:00"));
$endOfWeek = (date('l') == 'Sunday') ? date('d/m/Y 23:59:59') : date('d/m/Y', strtotime("sunday 23:59:59"));

#26


1  

A smart way of doing this is to let PHP handle timezone differences and Daylight Savings Time (DST). Let me show you how to do this.

一种巧妙的方法是让PHP处理时区差异和夏令时(DST)。让我告诉你怎么做。

This function will generate all days from Monday until Friday, inclusive (handy for generating work week days):

该功能将从周一到周五全天提供,包括(便于产生工作日):

class DateTimeUtilities {
    public static function getPeriodFromMondayUntilFriday($offset = 'now') {
        $now = new \DateTimeImmutable($offset, new \DateTimeZone('UTC'));
        $today = $now->setTime(0, 0, 1);

        $daysFromMonday = $today->format('N') - 1;

        $monday = $today->sub(new \DateInterval(sprintf('P%dD', $daysFromMonday)));
        $saturday = $monday->add(new \DateInterval('P5D'));

        return new \DatePeriod($monday, new \DateInterval('P1D'), $saturday);
    }
}

foreach (DateTimeUtilities::getPeriodFromMondayUntilFriday() as $day) {
    print $day->format('c');
    print PHP_EOL;
}

This will return datetimes Monday-Friday for current week. To do the same for an arbitrary date, pass a date as a parameter to DateTimeUtilities ::getPeriodFromMondayUntilFriday, thus:

这将会在周一返回datetimes周一到周五。对于任意日期执行相同的操作,将日期作为参数传递给DateTimeUtilities::getPeriodFromMondayUntilFriday,因此:

foreach (DateTimeUtilities::getPeriodFromMondayUntilFriday('2017-01-02T15:05:21+00:00') as $day) {
    print $day->format('c');
    print PHP_EOL;
}

//prints 
//2017-01-02T00:00:01+00:00
//2017-01-03T00:00:01+00:00
//2017-01-04T00:00:01+00:00
//2017-01-05T00:00:01+00:00
//2017-01-06T00:00:01+00:00

Only interested in Monday, as the OP asked?

只对周一感兴趣,就像OP问的那样?

$monday = DateTimeUtilities::getPeriodFromMondayUntilFriday('2017-01-02T15:05:21+00:00')->getStartDate()->format('c');
print $monday;
// prints
//2017-01-02T00:00:01+00:00

#27


0  

I've come against this question a few times and always surprised the date functions don't make this easier or clearer. Here's my solution for PHP5 that uses the DateTime class:

我已经对这个问题做了几次反驳,并且总是惊讶于日期函数并没有使它变得更简单或更清晰。这里是我的PHP5的解决方案,它使用DateTime类:

/**
 * @param DateTime $date A given date
 * @param int $firstDay 0-6, Sun-Sat respectively
 * @return DateTime
 */
function getFirstDayOfWeek(DateTime $date, $firstDay = 0) {
    $offset = 7 - $firstDay;
    $ret = clone $date;
    $ret->modify(-(($date->format('w') + $offset) % 7) . 'days');
    return $ret;
}

Necessary to clone to avoid altering the original date.

需要克隆以避免更改原始日期。

#28


0  

What about:

是什么:

$date = "2013-03-18"; 
$searchRow = date("d.m.Y", strtotime('last monday',strtotime($date." + 1 day")));
echo "for date " .$date ." we get this monday: " ;echo $searchRow; echo '<br>';

Its not the best way but i tested and if i am in this week i get the correct monday, and if i am on a monday i will get that monday.

这不是最好的方法,但我测试过,如果我在这周,我得到了正确的星期一,如果我在星期一,我会得到那个星期一。

#29


0  

I found this solution helpful. Just subtract if it isn't monday to get the previous Monday. I am using $lower_date as the date I pulled from a query that I then need to reconcile to the previous Monday.

我发现这个解决方案很有帮助。如果周一不是周一就做减法。我使用$lower_date作为我从查询中提取的日期,然后需要与前一个星期一进行协调。

//set this up to go backwards until you find monday
while(date('D',strtotime($lower_date))!='Mon'){
    $lower_date = date('Y-m-d', strtotime($lower_date . ' - 1 day')); //increase the upper spec
}

#30


0  

this one is prepared for today is monday

今天是星期一。

function lastMonday(\DateTime $date) {

    $timestamp = $date->getTimestamp();

    $monday = ( 1 == date( 'N', $timestamp ));

    if ($monday) {
        return $timestamp;
    } else {
        return strtotime( 'last monday', $timestamp );
    }
}

if you want it to get timestamp instead of DateTime change first two lines (get rid of date->getTimestamp) change them to just this

如果您希望它获得时间戳而不是DateTime更改前两行(删除日期->getTimestamp),将它们更改为这样。

function lastMonday($timestamp) {

and if you want it to input string change first two lines to this

如果你想让它输入字符串改变前两行。

function lastMonday($dateString) {

    $timestamp = strtotime($dateString);