PHP Laravel按周从数据库获取数据

时间:2019-03-18 08:45:04

标签: php mysql laravel

你好,我有这个代码

  // Get all the days date of past month for performance report
    $start_date = date("Y-m", strtotime("previous month")) ."-01";
    $start_time = strtotime($start_date);

    $end_time_1 = strtotime("+1 week", $start_time);

    for($i=$start_time; $i<$end_time_1; $i+=86400)
    {
    $list1[] = date('D, d F Y', $i);
    }

    $end_time_2 = strtotime("+1 week", $end_time_1);

    for($i=$end_time_1; $i<$end_time_2; $i+=86400)
    {
    $list2[] = date('D, d F Y', $i);
    }

    $end_time_3 = strtotime("+1 week", $end_time_2);

    for($i=$end_time_2; $i<$end_time_3; $i+=86400)
    {
    $list3[] = date('D, d F Y', $i);
    }

    $end_time_4 = strtotime("+1 week", $end_time_3);

    for($i=$end_time_3; $i<$end_time_4; $i+=86400)
    {
    $list4[] = date('D, d F Y', $i);
    }

    $performance_week_1 = \DB::table('accounts')
    ->whereBetween('created_at', [$start_date, $end_time_1]) // get week 1
    ->get();
    $performance_week_2 = \DB::table('accounts')
    ->whereBetween('created_at', [$end_time_1, $end_time_2]) // get week 2
    ->get();
    $performance_week_3 = \DB::table('accounts')
    ->whereBetween('created_at', [$end_time_2, $end_time_3]) // get week 3
    ->get();
    $performance_week_4 = \DB::table('accounts')
    ->whereBetween('created_at', [$end_time_3, $end_time_4]) // get week 4
    ->get();

它的作用是获取过去一个月中所有日期的所有日期 Report

问题是end_time_1返回

  

1549584000

我希望它返回$start_time返回的内容,但从本月开始+ 1周

  

2019-02-08

想法是从上个月开始每周从db获取一些数据并将其发布到表中   非常感谢

3 个答案:

答案 0 :(得分:2)

您可以为此使用ADDDATE() mysql函数,例如:

SELECT ADDDATE("2019-02-08", INTERVAL 7 DAY);

这将使日期增加7天,也可以是列的日期

PS。您应该为此使用laravel的DB:raw()函数

答案 1 :(得分:0)

更改一行以获得所需的日期格式:

$end_time_1 = date("Y-m-d", strtotime("+1 week", $start_time)); 

答案 2 :(得分:0)

你好,非常感谢我所做的帮助,它奏效了

我更改了->whereBetween('created_at', [$start_date, $end_time_1])
->whereBetween('created_at', [date('Y-m-d',$end_time_1), date('Y-m-d',$end_time_2)])    现在一切正常,非常感谢