[英]Get first day date of the week on a given date PHP
我有個約會2015-12-16
我想獲取我約會的當前周的第一天日期
這里2015-12-16
是在一年的第 51 周然后我想獲得第 51 周的第一天的日期(此處為2015-12-14
)
我怎么辦? 謝謝你
編輯:它必須在一年中有 53 周時起作用(例如在 2015 年)
您可以執行以下操作:
$dateTime = new DateTime("2015-12-16");
$weekNo = $dateTime->format("W");
$newDate = new DateTime();
$newDate->setISODate($dateTime->format("Y"), $weekNo);
例子:
http://sandbox.onlinephpfunctions.com/code/281a1ac298bfee8be421e333e4b7e92c6bb44d65
由於上述內容在某些情況下有點偏離,因此這里有一些更可靠的內容:
$dateTime = new DateTime("2016-01-01");
$dateTime->sub(new DateInterval("P".($dateTime->format("w")-1)."D")); //Since the weekdays are 1-based.
示例: http : //sandbox.onlinephpfunctions.com/code/c5cb0f077fa77974d977ddbffa6bc0b61f9d7851
$date = new \DateTime('2015-12-16');
echo $date->modify('last sunday +1 day')->format('Y-m-d');
如果算上周一到周日,這就是本周的開始。
嘗試這個:
date('Y-m-d', strtotime('This week', strtotime('2015-12-16')));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.