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

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

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

see my codes bellow:



        
12条回答
  •  庸人自扰
    2020-12-23 23:37

    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:

    $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
    

提交回复
热议问题