[英]Get first day date of the week on a given date PHP
I have a date 2015-12-16
我有个约会
2015-12-16
i want to get the date of the first day's date for the current week of my date我想获取我约会的当前周的第一天日期
here 2015-12-16
is in the week 51 of the year then i want to get the first day's date of the week 51 ( 2015-12-14
here)这里
2015-12-16
是在一年的第 51 周然后我想获得第 51 周的第一天的日期(此处为2015-12-14
)
how could i do it ?我怎么办? thank you
谢谢你
EDIT: it must work when there are 53 weeks in the year (like in 2015 for example)编辑:它必须在一年中有 53 周时起作用(例如在 2015 年)
You can do the following:您可以执行以下操作:
$dateTime = new DateTime("2015-12-16");
$weekNo = $dateTime->format("W");
$newDate = new DateTime();
$newDate->setISODate($dateTime->format("Y"), $weekNo);
Example:例子:
http://sandbox.onlinephpfunctions.com/code/281a1ac298bfee8be421e333e4b7e92c6bb44d65 http://sandbox.onlinephpfunctions.com/code/281a1ac298bfee8be421e333e4b7e92c6bb44d65
Since the above is a bit off in some cases here's something more reliable:由于上述内容在某些情况下有点偏离,因此这里有一些更可靠的内容:
$dateTime = new DateTime("2016-01-01");
$dateTime->sub(new DateInterval("P".($dateTime->format("w")-1)."D")); //Since the weekdays are 1-based.
Example: http://sandbox.onlinephpfunctions.com/code/c5cb0f077fa77974d977ddbffa6bc0b61f9d7851示例: http : //sandbox.onlinephpfunctions.com/code/c5cb0f077fa77974d977ddbffa6bc0b61f9d7851
$date = new \DateTime('2015-12-16');
echo $date->modify('last sunday +1 day')->format('Y-m-d');
This gets start of this week if you count monday to sunday.如果算上周一到周日,这就是本周的开始。
尝试这个:
date('Y-m-d', strtotime('This week', strtotime('2015-12-16')));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.