php - 在php中,如何获得上周的日期范围?

  显示原文与译文双语对照的内容
143 3

如何在php中获取上周的日期范围?

查看我的代码 bellow:

<?php 
 function get_last_week_dates(){
//how can i get the date range last week?
//ex: today is 2014-2-8
//the week date range of last week should be '2014-1-26 ~ 2014-2-1'
 }
?>
时间:原作者:0个回答

60 4

你可以使用 strtotime()

$previous_week = strtotime("-1 week +1 day");
$start_week = strtotime("last sunday midnight",$previous_week);
$end_week = strtotime("next saturday",$start_week);
$start_week = date("Y-m-d",$start_week);
$end_week = date("Y-m-d",$end_week);
echo $start_week.' '.$end_week ;

更新

更改了代码以处理星期日。如果当天是星期日,那么- 1周将是上周日,再次获得上周的星期日。

$previous_week = strtotime("-1 week +1 day");

另外,如果我们需要找到 current weeknext week 日期范围,我们可以

周-

$d = strtotime("today");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week); 
$end = date("Y-m-d",$end_week); 

下星期英镑

$d = strtotime("+1 week -1 day");
$start_week = strtotime("last sunday midnight",$d);
$end_week = strtotime("next saturday",$d);
$start = date("Y-m-d",$start_week); 
$end = date("Y-m-d",$end_week); 
原作者:
111 4

简单使用

date("m/d/Y", strtotime("last week monday"));
date("m/d/Y", strtotime("last week sunday"));

它将给出星期and星期日的日期。

原作者:
59 1

你需要使用 strtotime 函数进行这里操作

<center>
<?php
 function get_last_week_dates()
 {
//how can i get the date range last week?
//ex: today is 2014-2-8
//the week date range of last week should be '2014-1-26 ~ 2014-2-1'
 $startdate ="last monday";
 if (date('N')!== '1')
 { 
//it's not Monday today
 $startdate. =" last week";
 }
 echo"<br/>";
 $day = strtotime($startdate);
 echo date('r', $day); 
 echo"<br/>";
 $sunday = strtotime('next monday', $day) - 1;
 echo date('r', $sunday);
 }
 get_last_week_dates();
?>
</center>
原作者:
70 2

为了尝试解决这个问题的乐趣:

date_default_timezone_set('UTC');
$firstDayOfLastWeek = mktime(0,0,0,date("m"),date("d")-date("w")-7);
$lastDayOfLastWeek = mktime(0,0,0,date("m"),date("d")-date("w")-1);
echo("Last week began on:".date("d.m.Y",$firstDayOfLastWeek));
echo("<br>");
echo("Last week ended on:".date("d.m.Y",$lastDayOfLastWeek));
原作者:
131 4

这应该可以

$startWeek = date('Y-m-d',strtotime("Sunday Last Week"));
$endWeek = date('Y-m-d',strtotime("Sunday This Week"));

如果在星期一运行的话,这将不起作用。it星期日( 前一天) 到下周日。所以使用Abhik的chakraborty方法进行以下操作:

$startTime = strtotime("last sunday midnight",$previous_week);
$endTime = strtotime("next sunday midnight",$startTime);
$startDate = date('Y-m-d 00:00:00',$startTime);
$endDate = date('Y-m-d 23:59:59',$endTime);

这将给出

start = 2014-08-10 00:00:00
endDate = 2014-08-17 23:59:59
原作者:
...