duanlie1298 2015-06-06 17:52
浏览 19
已采纳

拆分数周到几周?

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.

  • 写回答

2条回答 默认 最新

  • dsfliu1129 2015-06-07 19:41
    关注

    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 :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀