在 PHP 中获取一周的第一天?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1897727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get first day of week in PHP?
提问by Iggy Ma
Given a date MM-dd-yyyyformat, can someone help me get the first day of the week?
给定日期MM-dd-yyyy格式,有人可以帮我获取一周的第一天吗?
回答by JadedCore
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'));
$daycontains a number from 0 to 6 representing the day of the week (Sunday = 0, Monday = 1, etc.).
$week_startcontains the date for Sunday of the current week as mm-dd-yyyy.
$week_endcontains the date for the Saturday of the current week as mm-dd-yyyy.
$day包含一个从 0 到 6 的数字,表示星期几(星期日 = 0,星期一 = 1 等)。
$week_start包含当前周星期日的日期为 mm-dd-yyyy。
$week_end包含当前周星期六的日期,如 mm-dd-yyyy。
回答by laurentb
strtotime('this week', time());
Replace time(). Next sunday/last monday methods won't work when the current day is sunday/monday.
替换时间()。当前日是星期日/星期一时,下一个星期日/上一个星期一的方法将不起作用。
回答by Raja Rama Mohan Thavalam
Very simple to use strtotimefunction:
非常简单的使用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:
Output for 5.3.0 - 5.6.6, php7@20140507 - 20150301, hhvm-3.3.1 - 3.5.1
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
4.3.5 - 5.2.17 的输出
2015-03-23
2015-03-22
Output for 4.3.0 - 4.3.4
4.3.0 - 4.3.4 的输出
2015-03-30
2015-03-29
Comparing at Edge-Cases
比较边缘情况
Relative descriptions like this weekhave their own context. The following shows the output for this weekmonday 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
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
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
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
4.3.0 - 4.3.4 的输出
2015-03-23
2015-03-29
2015-03-30
2015-03-29
回答by job3dot5
Keep it simple :
把事情简单化 :
<?php
$dateTime = new \DateTime('2020-04-01');
$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 手册
NB: as some user commented the $dateTime value will be modified.
注意:正如一些用户评论的那样, $dateTime 值将被修改。
回答by Yuvalik
$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= 星期日和6= 星期六。从那里你可以简单地向后计算你想要的那一天。
回答by AgmLauncher
EDIT: the below link is no longer running on the version of PHP stated. It is running on PHP 5.6 which improves the reliability of strtotime, but isn't perfect! The results in the table are live results from PHP 5.6.
编辑:以下链接不再在声明的 PHP 版本上运行。它在 PHP 5.6 上运行,提高了 strtotime 的可靠性,但并不完美!表中的结果是 PHP 5.6 的实时结果。
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");
回答by vascowhite
This question needs a good DateTimeanswer:-
这个问题需要一个很好的DateTime答案:-
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.
这将处理年份边界和闰年。
回答by Malagana
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
回答by shasi kanth
Assuming Monday as the first day of the week, this works:
假设星期一为一周的第一天,这有效:
echo date("M-d-y", strtotime('last monday', strtotime('next week', time())));
回答by birkof
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()));

