根据PHP中的日期过滤器过滤结果数组

时间:2019-12-16 13:19:29

标签: php arrays laravel

我有一个从mysql数据库获取的结果数组。我想根据以下日期对这些数据进行分组:

输入数组:

$resultSet = [
   0 => [
     'id' => 123,
     'name' => 'ABC',
     'start_date' => '2019-12-18 20:00:00'
   ], 
   1 => [
     'id' => 124,
     'name' => 'CDE',
     'start_date' => '2019-12-19 20:00:00'
   ],  
   2 => [
     'id' => 125,
     'name' => 'TEST',
     'start_date' => '2019-12-23 20:00:00'
   ],
   3 => [
     'id' => 126,
     'name' => 'BWM',
     'start_date' => '2019-12-18 20:00:00'
   ], 
   4 => [
     'id' => 127,
     'name' => 'XYZ',
     'start_date' => '2019-12-19 20:00:00'
   ], 
   5 => [
     'id' => 128,
     'name' => 'GHJ',
     'start_date' => '2019-12-21 20:00:00'
   ], 
   6 => [
     'id' => 129,
     'name' => 'GHJK',
     'start_date' => '2019-12-22 20:00:00'
   ], 
   7 => [
     'id' => 130,
     'name' => 'GHL',
     'start_date' => '2019-12-20 20:00:00'
   ], 
   8 => [
     'id' => 131,
     'name' => 'JKL',
     'start_date' => '2019-12-25 20:00:00'
   ]
];

输出:按过滤器显示产品组的所有列表(今天,明天,本周末,下周末等)。考虑我们在星期三运行脚本。

今天:仅显示索引记录:0、3(今天的日期:2019-12-18)

明天:仅显示索引记录:0、3(明天日期:2019-12-19)

本周末:仅显示索引记录:5、6、7(周五,周六和周日:2019-12-20、2019-12-21、2019-12-22)< / p>

下周:仅显示索引记录:2、8(日期:2019-12-23、2019-12-25)

结果将动态显示,并且每周都会显示不同的记录,如上。

使用的示例代码:

$arrayList = [];
 foreach($resultSet as $element){
   $arrayList[$element->start_date][] = $element;
 }

 dd($arrayList);

需要更改上面的代码以按照以下要求获取结果。

框架:Laravel 5.2

要求

  1. 脚本将在星期三或星期四(90%的时间)运行。
  2. 以防万一那些天脚本没有运行。我们将在星期五手动运行它。在这种情况下,今天是星期五,而星期六是明天。另外,本周末将包括星期五,星期六和星期日的数据。
  3. 本周末将包括星期五,星期六和星期日的数据。
  4. 今天,明天,本周末和下一个周末标签将从“ 开始日期”中过滤掉。

预期产量

$output = [
   'today' => [
       0 => [
         'id' => 123,
         'name' => 'ABC',
         'start_date' => '2019-12-18 20:00:00'
       ],
       1 => [
         'id' => 126,
         'name' => 'BWM',
         'start_date' => '2019-12-18 20:00:00'
       ]
    ],
    'tomorrow' => [
       0 => [
         'id' => 124,
         'name' => 'CDE',
         'start_date' => '2019-12-19 20:00:00'
       ],
       1 =>  [
         'id' => 127,
         'name' => 'XYZ',
         'start_date' => '2019-12-19 20:00:00'
       ]
    ],

    'this_weekend' => [
       0 => [
         'id' => 130,
         'name' => 'GHL',
         'start_date' => '2019-12-20 20:00:00'
       ],
       1 =>  [
         'id' => 128,
         'name' => 'GHJ',
         'start_date' => '2019-12-21 20:00:00'
       ],
       2 =>  [
         'id' => 129,
         'name' => 'GHJK',
         'start_date' => '2019-12-22 20:00:00'
       ]
    ],
    'next_week' => [
       0 => [
         'id' => 125,
         'name' => 'TEST',
         'start_date' => '2019-12-23 20:00:00'
       ],
       1 => [
         'id' => 131,
         'name' => 'JKL',
         'start_date' => '2019-12-25 20:00:00'
       ]
     ]
  ];    

我正在视图页面中运行简单的foreach循环以显示数据。如有任何疑问,请在下面发表评论。

4 个答案:

答案 0 :(得分:1)

一开始,您应该定义一个有用的日期格式,以便进一步比较日期:

 $s = new DateTime();                         // create date value
 $s->modify('+2 days');                       // in fact today is 2019-12-16
                                              // but you've choose 2019-12-18
                                              // you can comment this line
 //echo $s->format('Y-m-d H:i:s').PHP_EOL;    // today
 $today = $s->format('Ymd');                  // format 20191218

 echo $today.' <- today'.PHP_EOL;             // today

 $nextmonday = $s->modify('next monday')->format('Ymd');
 echo $nextmonday.' <- next monday'.PHP_EOL.PHP_EOL;     // next monday, format 20191223

然后您需要像这样循环foreach

foreach($resultSet as $rec){
    $d = date('Ymd', strtotime($rec['start_date']));      // transform to format 20191218 
    $d2 = date('l', strtotime($rec['start_date']));       // name of the day
    echo $d.' --- '.$d2.PHP_EOL;    

    if ($d == $today) {                 // 2019-12-18 == 2019-12-18
        $res['today'][] = $rec;
    } else if ($d - $today == 1) {      // 2019-12-19 - 2019-12-18 == 1
        $res['tomorrow'][] = $rec;
    } else if (in_array($d2,['Friday','Saturday','Sunday']) && $d < $nextmonday){    // < 2019-12-23 and by day name
        $res['this_weekend'][] = $rec;
    } else if ($d >= $nextmonday){       // next week (not a weekend)
        $res['next_weekend'][] = $rec;
    } 
}

输出将是:

Array
(
    [today] => Array
        (
            [0] => Array
                (
                    [id] => 123
                    [name] => ABC
                    [start_date] => 2019-12-18 20:00:00
                )

            [1] => Array
                (
                    [id] => 126
                    [name] => BWM
                    [start_date] => 2019-12-18 20:00:00
                )

        )

    [tomorrow] => Array
        (
            [0] => Array
                (
                    [id] => 124
                    [name] => CDE
                    [start_date] => 2019-12-19 20:00:00
                )

            [1] => Array
                (
                    [id] => 127
                    [name] => XYZ
                    [start_date] => 2019-12-19 20:00:00
                )

        )

    [next_weekend] => Array
        (
            [0] => Array
                (
                    [id] => 125
                    [name] => TEST
                    [start_date] => 2019-12-23 20:00:00
                )

            [1] => Array
                (
                    [id] => 131
                    [name] => JKL
                    [start_date] => 2019-12-25 20:00:00
                )

        )

    [this_weekend] => Array
        (
            [0] => Array
                (
                    [id] => 128
                    [name] => GHJ
                    [start_date] => 2019-12-21 20:00:00
                )

            [1] => Array
                (
                    [id] => 129
                    [name] => GHJK
                    [start_date] => 2019-12-22 20:00:00
                )

            [2] => Array
                (
                    [id] => 130
                    [name] => GHL
                    [start_date] => 2019-12-20 20:00:00
                )

        )

)

注意: 最后一个名为 next_weekend 的数组,也许您需要将其重命名为 next_week < em>,因为它的日期刚好在您当前的周末之后。

我认为您已经理解了一个主意,并且可以添加进一步的逻辑。

Demo

答案 1 :(得分:0)

尝试下面的示例

$group = [];
    foreach ($start_date as $item)  {
        if (!isset($group[$item['start_date']])) {
            $group[$item['start_date']] = [];
        }
        foreach ($item as $key => $value) {
            if ($key == 'start_date') continue;
            $group[$item['start_date']][$key] = $value;
        }
    }

答案 2 :(得分:0)

我认为您应该设置一系列不同的日期:今天,明天,本周末,下周

检查链接以获取时间格式:https://carbon.nesbot.com/docs/

$now = Carbon::now();
echo $now;                               // 2019-11-20 16:41:03
echo "\n";
$today = Carbon::today();
echo $today;                             // 2019-11-20 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2019-11-21 00:00:00
echo "\n";

通过比较日期条件检查结果并创建数组并将其组合为一个数组。

$totalarr = array('today'=>$todayarr,'tomorrow'=>$tomorrowarr,'thisweek'=>$thisweek,'nextweek'=>$nextweekarr);

print_r($totalarr);

答案 3 :(得分:0)

好吧,如果您使用的是laravel,那么您可以利用碳的力量和收集方法来解决此类问题。

您可以映射阵列键并使用carbon方法对其进行分组。检查以下代码片段将为您提供准确的结果。

$output = collect($resultSet)->map(function ($value) {
    $group_by_key = Carbon::parse($value['start_date']);
    switch ($group_by_key) {
        case $group_by_key->isToday():
            $group_by_key = 'today';
            break;
        case $group_by_key->isTomorrow():
            $group_by_key = 'tomorrow';
            break;
        case $group_by_key->isWeekend():
            $group_by_key = 'this_weekend';
            break;
        case $group_by_key->isNextWeek():
            $group_by_key = 'next_week';
            break;

        default:
            $group_by_key = $group_by_key->format('l');;
            break;
    }
    $value['group_by_key'] = $group_by_key;
    return $value;
})->groupBy('group_by_key');
var_dump($output);