如何获取周日和周末日期的开始日期星期六星期六



如何获取周日和周日星期六的周日开始日期。

我在下面尝试自我来代码,以获取一周的开始日期作为周一的周日末日为周日。

$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的解决方案,与datestrtotime相比,使用的方法还要好一些:

$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"));

最新更新