任何人都可以帮我找到如何使用currentdate找到上周的开始日期或结束日期
假设我今天的日期是: -
enter code here
date('y-m-d') = 2017-01-31 today is Tuesday
所以我希望上周的开始日期和结束日期输出如
enter code here
last Week starting date:- 2017-01-23
last week ending date :- 2017-01-29
任何人都可以告诉我如何做到这一点
答案 0 :(得分:2)
首先得到周数而不是前一周并得到它的日期。
$ddate = "2017-1-31";
$date = new DateTime($ddate);
$week = $date->format("W");
$year = $date->format("o");
var_dump($week);
function etStartAndEndDate($week, $year) {
$dto = new DateTime();
$dto->setISODate($year, $week);
$ret['week_start'] = $dto->format('Y-m-d');
$dto->modify('+6 days');
$ret['week_end'] = $dto->format('Y-m-d');
return $ret;
}
$day=etStartAndEndDate($week-1,$year);
var_dump($day)
答案 1 :(得分:1)
您可以通过以下方式轻松使用date和strtotime功能:
echo "today is: ".date("m/d/Y", strtotime("today"))."<br/>";
echo "last Week starting date: ".date("m/d/Y", strtotime("last week monday"))."<br/>";
echo "last Week ending date: ".date("m/d/Y", strtotime("last week sunday"));
答案 2 :(得分:1)
我已检查过示例..请尝试这可以帮助您。
date_default_timezone_set('UTC');
$firstDayOfLastWeek = mktime(0,0,0,date("m"),date("d")-date("w")-6);
$lastDayOfLastWeek = mktime(0,0,0,date("m"),date("d")-date("w")-0);
echo("Last week began on: ".date("d.m.Y",$firstDayOfLastWeek));
echo("<br>");
echo("Last week ended on: ".date("d.m.Y",$lastDayOfLastWeek));