秒杀日期思想与赋值

看了上面的效果有没有想过是怎么做到的?

public function search()
{
    if (!$this->validate()) {
        return $this->errorResponse;
    }
    list($begin_date, $end_date) = self::getDate($this->month);
    $query = MiaoshaGoods::find()->alias('mg')
        ->select('mg.open_date,COUNT(mg.id) as miaosha_count')
        ->where([
            'AND',
            ['mg.is_delete' => 0, 'mg.store_id' => $this->store_id,],
            ['>=', 'mg.open_date', $begin_date],
            ['<=', 'mg.open_date', $end_date],
        ])->groupBy('mg.open_date')->orderBy('mg.open_date ASC');
    $list = $query->asArray()->all();
    $new_list = [];
    foreach ($list as $item) {
        $new_list[str_replace('-', '', $item['open_date'])] = $item;     //值得学习的地方!!!
    }
    //file_put_contents("log.txt", print_r($new_list,1).PHP_EOL, FILE_APPEND);
    return [
        'code' => 0,
        'data' => [
            'list' => $new_list,
        ],
    ];
}

//这个方法就可实现每月的开始日期和结束日期不怕搞错,很好!!!
public static function getDate($month)
{
    $month = intval($month);
    $begin_date = date('Y-m-d', strtotime(date('Y-m-01') . " {$month} month"));
    $end_date = date('Y-m-d', strtotime(date('Y-m-01') . " " . ($month + 1) . " month -1 day"));
    return [$begin_date, $end_date];
}

那个值得学习的地方!!!你猜他的效果是怎样的?其实值不会覆盖!

秒杀日期思想与赋值

 

相关文章:

  • 2021-11-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-09
  • 2022-01-16
  • 2022-01-11
  • 2021-09-05
猜你喜欢
  • 2022-02-16
  • 2021-08-30
  • 2021-07-03
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案