如何在星期日和星期六结束日期在PHP

时间:2015-05-05 14:58:13

标签: php date

如何在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"));

谢谢, 瓦森特

3 个答案:

答案 0 :(得分:1)

以下是使用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; disable :)相比,使用起来更好一点:

$(this).cropper('disable');

答案 1 :(得分:0)

更改您的本地配置:

setlocale('LC_TIME', 'fr_FR.utf8');

更改' fr_FR.utf8'由您当地人。

答案 2 :(得分:0)

我改变了我的代码,如下所示。这段代码是对的吗?但它对我有用。

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