[英]How to get week start date Sunday and week end date Saturday in php
如何在php中獲取周日開始日期和周日結束日期。
我在下面嘗試通過自我編碼將“周開始日期”作為“星期一”,“周結束日期”作為“星期日”。
$cdate = date("Y-m-d");
$week = date('W', strtotime($cdate));
$year = date('Y', strtotime($cdate));
$firstdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}+1"));
$lastdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}-7"));
謝謝,Vasanth
這是一個使用DateTime
的解決方案,它比date
和strtotime
更好一些:):
$today = new \DateTime();
$currentWeekDay = $today->format('w'); // Weekday as a number (0 = Sunday, 6 = Saturday)
$firstdayofweek = clone $today;
$lastdayofweek = clone $today;
if ($currentWeekDay !== '0') {
$firstdayofweek->modify('last sunday');
}
if ($currentWeekDay !== '6') {
$lastdayofweek->modify('next saturday');
}
echo $firstdayofweek->format('Y-m-d').PHP_EOL;
echo $lastdayofweek->format('Y-m-d').PHP_EOL;
更改您的本地配置:
setlocale('LC_TIME', 'fr_FR.utf8');
根據您所在的地方更改“ fr_FR.utf8”。
我如下更改了代碼。 此代碼正確嗎? 但這對我有用。
$cdate = date("Y-m-d");
$week = date('W', strtotime($cdate));
$year = date('Y', strtotime($cdate));
echo "<br>".$firstdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}-0"));
echo "<br>".$lastdayofweek = date("Y-m-d", strtotime("{$year}-W{$week}-6"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.