mohamed adel
mohamed adel

Reputation: 715

PHP Laravel get data by week from db

Hello i have this code

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

What it does is it gets all the date of the days of the past month Report

The problem is end_time_1 return

1549584000

i want it to return what $start_time returns but + 1 week from this month

2019-02-08

The idea is to get somedata from db by every week from the last month and post it to table Thank you very much

Upvotes: 0

Views: 791

Answers (3)

mohamed adel
mohamed adel

Reputation: 715

Hello Thanks very much for the help i did that and it worked

i changed ->whereBetween('created_at', [$start_date, $end_time_1])
to ->whereBetween('created_at', [date('Y-m-d',$end_time_1), date('Y-m-d',$end_time_2)]) Now it works just fine thank you very much

Upvotes: 0

neha gupta
neha gupta

Reputation: 30

CHange one line to get required format of date:

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

Upvotes: 0

Terry
Terry

Reputation: 332

you can use the ADDDATE() mysql function for that, for example:

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

That will add 7 days to the date, this can also be the date of column

PS. you should use laravel's DB:raw() function for this

Upvotes: 2

Related Questions