如何在php中获取周日开始日期和周日结束日期 [英] How to get week start date Sunday and week end date Saturday in php
本文介绍了如何在php中获取周日开始日期和周日结束日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在php中获取周日开始日期和周日结束日期。
How to get week start date Sunday and week end date Saturday in php.
我尝试通过下面的代码将周开始日期作为星期一,将周结束日期作为星期日。
I try my self below to code am getting Week Start Date as Monday Week End Date as Sunday.
$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
Thanks, Vasanth
推荐答案
这是使用 DateTime
的解决方案,它比 date更好用
和 strtotime
:):
Here is a solution using DateTime
, which is a little bit nicer to work with than date
and 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;
这篇关于如何在php中获取周日开始日期和周日结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文