我怎样才能获得上周的PHP日期范围?

时间:2014-02-08 08:55:10

标签: php date

我怎么能在上周获得#39; php中的日期范围?

请看下面的代码:

<?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'
    }
?>

12 个答案:

答案 0 :(得分:76)

您可以使用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); 

答案 1 :(得分:20)

只需使用

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

它将给出上周的周一和周日的日期。

答案 2 :(得分:2)

您需要使用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>

答案 3 :(得分: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));

答案 4 :(得分:1)

这应该可以解决问题

$startWeek = date('Y-m-d',strtotime("Sunday Last Week"));
$endWeek = date('Y-m-d',strtotime("Sunday This Week"));
如果在星期一运行,这将无效。它会在上周日(前一天)到下一个星期天。因此,使用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

答案 5 :(得分:1)

我知道这已经过时了,但这是一种更为简洁的方式:

$startDate = date("m/d/y", strtotime(date("w") ? "2 sundays ago" : "last sunday"));
$endDate = date("m/d/y", strtotime("last saturday"));
echo $startDate . " - " . $endDate

答案 6 :(得分:0)

你可以这样做。

首先获取当前时间戳并减去所需的天数。

$curTime = time();
echo date("Y-m-d",$curTime);
echo "<br />";
echo date("Y-m-d",($curTime-(60*60*24*7)));

答案 7 :(得分:0)

$lastWeekStartTime = strtotime("last sunday",strtotime("-1 week"));
$lastWeekEndTime = strtotime("this sunday",strtotime("-1 week"));
$lastWeekStart = date("Y-m-d",$lastWeekStartTime);
$lastWeekEnd = date("Y-m-d",$lastWeekEndTime);

答案 8 :(得分:0)

为了找到上周的开始日期和结束日期,您可以按照此代码进行操作。

它适用于所有时间间隔以查找日期间隔。

$Current = Date('N');
$DaysToSunday = 7 - $Current;
$DaysFromMonday = $Current - 1;
$Sunday = Date('d/m/y', strtotime("+ {$DaysToSunday} Days"));
$Monday = Date('d/m/y', strtotime("- {$DaysFromMonday} Days"));

如果需要,您需要使用datatime()进行更改,您可以执行此功能。

$date = new DateTime();
$weekday = $date->format('w');
$diff = 7 + ($weekday == 0 ? 6 : $weekday - 1); // Monday=0, Sunday=6
$date->modify("-$diff day");
echo $date->format('Y-m-d') . ' - ';
$date->modify('+6 day');
echo $date->format('Y-m-d');
  

使用功能:

如果你想在功能的帮助下找到最后一周的范围,你可以这样做。

<强>功能:

// returns last week's range
function last_week_range($date) {
$ts = strtotime("$date - 7 days");
$start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);
return array(
      date('Y-m-d', $start),
      date('Y-m-d', strtotime('next saturday', $start))
);
}

<强>用法:

$today=date();
print_r(last_week_range($today));

所有上述功能都会返回上周的范围而不管一周的开始日期。

答案 9 :(得分:0)

这个将产生正确的结果并处理周一问题

<?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 ";
?>

答案 10 :(得分:0)

提供的其他大多数解决方案都在一天之内关闭。
如果您想在上周的周日至周六进行操作,这就是方法。

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

答案 11 :(得分:0)

$startOfTheWeek = Carbon::now()->subWeek(1)->startOfWeek();
$endOfTheWeek = Carbon::now()->subWeek(1)->endOfWeek();

从特定日期开始

$startOfTheWeek = Carbon::parse('2020-03-02')->subWeek(1)->startOfWeek();
$endOfTheWeek = Carbon::parse('2020-03-02')->subWeek(1)->endOfWeek();

考虑一周从星期一开始,到星期日结束。