php 我怎样才能在php中获得上周的日期范围?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21644002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I get last week' date range in php?
提问by mingfish_004
how can I get last week' date range in php ?
我怎样才能在 php 中获得上周的日期范围?
see my codes 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'
}
?>
回答by Abhik Chakraborty
You can use strtotime()
您可以使用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 ;
UPDATE
更新
Changed the code to handle sunday. If the current day is sunday then - 1 week will be previous sunday and again getting previous sunday for that will go the one week back.
更改了代码以处理星期日。如果当天是星期日,那么 - 1 周将是前一个星期日,再次获得前一个星期日将返回一周。
$previous_week = strtotime("-1 week +1 day");
In addition if we need to find the
current weekandnext weekdate range we can do as
另外,如果我们需要找到
current week和next week我们所能做的日期范围
Current 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);
Next 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);
回答by Pankaj Samal
Simply use
只需使用
date("m/d/Y", strtotime("last week monday"));
date("m/d/Y", strtotime("last week sunday"));
It will give the date of Last week's Monday and Sunday.
它将给出上周的星期一和星期日的日期。
回答by Ferrakkem Bhuiyan
you need you use strtotimefunction for this
你需要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>
回答by ElDoRado1239
Well just for the fun of trying to solve this:
好吧只是为了尝试解决这个问题的乐趣:
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));
回答by Jason Bruce
This should do the trick
这应该可以解决问题
$startWeek = date('Y-m-d',strtotime("Sunday Last Week"));
$endWeek = date('Y-m-d',strtotime("Sunday This Week"));
this would not work if ran on a Monday. It would get last Sunday (the day before) to the next Sunday. So using Abhik Chakraborty's method with the above:
如果在星期一运行,这将不起作用。它会从上周日(前一天)到下周日。因此,将 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);
This will now give
这现在会给
start = 2014-08-10 00:00:00
endDate = 2014-08-17 23:59:59
回答by Lifz
I know this is old but here's a much more succinct way of doing it:
我知道这是旧的,但这里有一个更简洁的方法:
$startDate = date("m/d/y", strtotime(date("w") ? "2 sundays ago" : "last sunday"));
$endDate = date("m/d/y", strtotime("last saturday"));
echo $startDate . " - " . $endDate
回答by Kavvson Empcraft
This one will produce the proper result and handles the Monday issue
这将产生正确的结果并处理星期一问题
<?php
$monday = strtotime("last monday");
$monday = date('W', $monday)==date('W') ? $monday-7*86400 : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);
echo "Last week range from $this_week_sd to $this_week_ed ";
?>
回答by Vincent
Most of those other solutions offered were off by one day.
If you want Sunday to Saturday for last week, this is the way to do it.
提供的大多数其他解决方案都在一天前关闭。
如果您想要上周的周日到周六,这就是这样做的方法。
$start = date("Y-m-d",strtotime("last sunday",strtotime("-1 week")));
$end = date("Y-m-d",strtotime("saturday",strtotime("-1 week")));
echo $start. " to ".$end;
回答by f_i
Carbon
碳
$startOfTheWeek = Carbon::now()->subWeek(1)->startOfWeek();
$endOfTheWeek = Carbon::now()->subWeek(1)->endOfWeek();
From a specific date
从特定日期开始
$startOfTheWeek = Carbon::parse('2020-03-02')->subWeek(1)->startOfWeek();
$endOfTheWeek = Carbon::parse('2020-03-02')->subWeek(1)->endOfWeek();
Considering the week starts at Monday and ends at Sunday.
考虑到一周从星期一开始,到星期日结束。
回答by Rocky
You can do this way.
你可以这样做。
First get the current timestamp and subtract the no.of days you want.
首先获取当前时间戳并减去您想要的天数。
$curTime = time();
echo date("Y-m-d",$curTime);
echo "<br />";
echo date("Y-m-d",($curTime-(60*60*24*7)));

