PHP 实现以日期分组的浏览足迹【运用场景:电商项目=》我的足迹

2020-05-13  本文已影响0人  mike_lp

一、场景

场景一:

在电商项目中,用户的个人中心处,会存在我的历史足迹的入口,查看时,是以时间进行分组,倒排展示。

场景二:

在资源社区平台,用户中心处,会存在我的浏览足迹入口,查看时,也是以时间进行分组,倒排展示。

实现过程

通过数据库查询时,获取的内容是这样的

   array:5 [▼
    0 => array:6 [▼
        "id" => 20
        "user_id" => 200000000003
        "title" => "无人驾驶面临多项发展问题 L5级汽车或于2030年上路"
        "content_id" => 140000001153
        "content_type" => 14
        "save_time" => "2020-05-11T07:14:16.000000Z"
    ]
    1 => array:6 [▼
        "id" => 19
        "user_id" => 200000000003
        "title" => "汽车车灯控制系统的VHDL语言实现"
        "content_id" => 100000000850
        "content_type" => 10
        "save_time" => "2020-05-11T07:14:10.000000Z"
    ]
    2 => array:6 [▼
        "id" => 16
        "user_id" => 200000000003
        "title" => "BYTON推出首款智能电动车 2019年可以实现全面量产"
        "content_id" => 140000000380
        "content_type" => 14
        "save_time" => "2020-05-11T06:44:21.000000Z"
    ]
    3 => array:6 [▼
        "id" => 14
        "user_id" => 200000000003
        "title" => "TCL公布了其10系列智能手机的价格和规格"
        "content_id" => 140000002546
        "content_type" => 14
        "save_time" => "2020-05-11T06:37:13.000000Z"
    ]
    4 => array:6 [▼
        "id" => 8
        "user_id" => 200000000003
        "title" => "苹果意外在删除的支持视频中确认类似Tile的AirTag名称"
        "content_id" => 140000002534
        "content_type" => 14
        "save_time" => "2020-05-11T03:00:54.000000Z"
    ]
]

我们期望的效果是这样的

array:1 [▼
  "05月11日" => array:6 [▼
  0 => array:6 [▼
  "id" => 41
  "user_id" => 200000000003
  "title" => "字号大小设置"
  "content_id" => 100000024839
  "content_type" => 10
  "save_time" => "2020-05-11T10:35:14.000000Z"
]
1 => array:6 [▼
  "id" => 20
  "user_id" => 200000000003
  "title" => "无人驾驶面临多项发展问题 L5级汽车或于2030年上路"
  "content_id" => 140000001153
  "content_type" => 14
  "save_time" => "2020-05-11T07:14:16.000000Z"
]
2 => array:6 [▼
  "id" => 19
  "user_id" => 200000000003
  "title" => "汽车车灯控制系统的VHDL语言实现"
  "content_id" => 100000000850
  "content_type" => 10
  "save_time" => "2020-05-11T07:14:10.000000Z"
]
3 => array:6 [▼
  "id" => 16
  "user_id" => 200000000003
  "title" => "BYTON推出首款智能电动车 2019年可以实现全面量产"
  "content_id" => 140000000380
  "content_type" => 14
  "save_time" => "2020-05-11T06:44:21.000000Z"
]
4 => array:6 [▼
  "id" => 14
  "user_id" => 200000000003
  "title" => "TCL公布了其10系列智能手机的价格和规格"
  "content_id" => 140000002546
  "content_type" => 14
  "save_time" => "2020-05-11T06:37:13.000000Z"
]
5 => array:6 [▼
  "id" => 8
  "user_id" => 200000000003
  "title" => "苹果意外在删除的支持视频中确认类似Tile的AirTag名称"
  "content_id" => 140000002534
  "content_type" => 14
  "save_time" => "2020-05-11T03:00:54.000000Z"
]
]  

实现函数

if(!function_exists('group_by_time')){
function group_by_time($array){
    $curyear = date('Y');
    $list = [];
    foreach ($array as $k => $v) {
        if ($curyear == date('Y',strtotime($v['save_time']))) {
            $date = date('m月d日',strtotime($v['save_time']));
        } else {
            $date = date('Y年m月d日',strtotime($v['save_time']));
        }
        $list[$date][] = $v;
    }
    return $list;
}

}

上一篇下一篇

猜你喜欢

热点阅读