拆分数周到几周?

I am returning an array from a database which contains a date field. I would like to split the array into weeks for example 'this week', 'next week'.

A lot of the examples I can find online are relative to today's date which is not what I want.

Today is 06/05/15, so this week started 01/05/15 and ends 07/05/15 .. I want to pluck out all entries that fall between these dates into their own array. Next week starts on 08/05/15 and ends 14/05/15, I want to pluck all dates that feel between these dates into a separate array.

If you are grouping your result set into three categories - This week, Next week and Upcoming - you just need to know is the start of the next week and the week after. You can then iterate over your result set, compare and group accordingly.

Judging from your comments above you are starting the week on Monday, so:

$dateTime       = new DateTime('next monday');
$weekStartNext  = $dateTime->format('Y-m-d H:i:s');
$weekStartAfter = $dateTime->add(new DateInterval('P7D'))->format('Y-m-d H:i:s');

You now have ISO8601 format datetime strings for the next Monday and the Monday after. So, given a result set like this:

$rows = [
    [
        'name'       => 'Sunday 7th June',
        'start_date' => '2015-06-07 00:00:00',
    ],
    [
        'name'       => 'Monday 8th June',
        'start_date' => '2015-06-08 00:00:00',
    ],
    [
        'name'       => 'Friday 12th June',
        'start_date' => '2015-06-12 00:00:00',
    ],
    [
        'name'       => 'Sunday 14th June',
        'start_date' => '2015-06-14 00:00:00',
    ],
    [
        'name'       => 'Monday 15th June',
        'start_date' => '2015-06-15 00:00:00',
    ],
    [
        'name'       => 'Sunday 21st June',
        'start_date' => '2015-06-21 00:00:00',
    ],
    [
        'name'       => 'Tuesday 30th June',
        'start_date' => '2015-06-30 00:00:00',
    ],
    [
        'name'       => 'Wednesday 1st July',
        'start_date' => '2015-07-01 00:00:00',
    ],
];

You can do simple string comparison on the row dates, like so:

$sorted = [];

foreach ($rows as $row) {

    $startDate = $row['start_date'];

    if ($startDate < $weekStartNext) {
        $sorted['This week'][] = $row;
    } elseif ($startDate >= $weekStartNext && $startDate < $weekStartAfter) {
        $sorted['Next week'][] = $row;
    } else {
        $sorted['Upcoming'][] = $row;
    }
}

print_r($sorted);

This yields:

Array
(
    [This week] => Array
        (
            [0] => Array
                (
                    [name] => Sunday 7th June
                    [start_date] => 2015-06-07 00:00:00
                )

        )

    [Next week] => Array
        (
            [0] => Array
                (
                    [name] => Monday 8th June
                    [start_date] => 2015-06-08 00:00:00
                )

            [1] => Array
                (
                    [name] => Friday 12th June
                    [start_date] => 2015-06-12 00:00:00
                )

            [2] => Array
                (
                    [name] => Sunday 14th June
                    [start_date] => 2015-06-14 00:00:00
                )

        )

    [Upcoming] => Array
        (
            [0] => Array
                (
                    [name] => Monday 15th June
                    [start_date] => 2015-06-15 00:00:00
                )

            [1] => Array
                (
                    [name] => Sunday 21st June
                    [start_date] => 2015-06-21 00:00:00
                )

            [2] => Array
                (
                    [name] => Tuesday 30th June
                    [start_date] => 2015-06-30 00:00:00
                )

            [3] => Array
                (
                    [name] => Wednesday 1st July
                    [start_date] => 2015-07-01 00:00:00
                )

        )

)

Hope this helps :)

This is a starting point to detect if a given unix date $date is within this week:

if (($date >= strtotime('monday this week')) and ($date <= strtotime('sunday this week'))) {
    // $date is within this week
}

Relevant documentation: strtotime