简体   繁体   English

我怎样才能在php中获得上周的日期范围?

[英]how can I get last week' date range in php?

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

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 week and next week date range we can do as另外,如果我们需要找到current weeknext 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); 

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.它将给出上周的星期一和星期日的日期。

you need you use strtotime function 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>

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

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

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

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

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

In order to find the last week start date and end date you can follow up this code for doing it so.为了找到上周的开始日期和结束日期,您可以按照此代码进行操作。

It works on all intervals to find the date interval.它适用于所有时间间隔以查找日期间隔。

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

If so you need to change it with the datatime() you can perform this function.如果是这样,您需要使用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');

Using Functions:使用功能:

If you want to find the last week range with the help of the functions you can preform like this.如果您想在函数的帮助下找到上周的范围,您可以像这样执行。

Function:功能:

// 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))
);
}

Usage:用法:

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

All the above functions that has been given will return the last week range irrespective of the start day of the week..上面给出的所有函数都将返回上周的范围,而不管一周的开始日期。

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;

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.考虑到一周从星期一开始,到星期日结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM