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

后端 未结 12 1242
轻奢々
轻奢々 2020-12-23 23:23

how can I get last week\' date range in php ?

see my codes bellow:



        
相关标签:
12条回答
  • 2020-12-23 23:42

    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.

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

    0 讨论(0)
  • 2020-12-23 23:42

    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.

    0 讨论(0)
  • 2020-12-23 23:43

    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)));
    
    0 讨论(0)
  • 2020-12-23 23:44

    you need you use strtotime function for this

    <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>
    
    0 讨论(0)
  • 2020-12-23 23:44

    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));
    
    0 讨论(0)
  • 2020-12-23 23:48
    $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);
    
    0 讨论(0)
提交回复
热议问题