where($where)->select();"."/> where($where)->select();".">

Home  >  Article  >  Backend Development  >  How to query this year's data in php

How to query this year's data in php

藏色散人
藏色散人Original
2021-12-20 09:50:182485browse

How to query this year's data in php: 1. Get the time of this year through date; 2. Run the query statement "$result = $db->where($where)->select(); "That's it.

How to query this year's data in php

#The operating environment of this article: Windows 7 system, thinkphp v5.1 version, Dell G3 computer.

How to query this year's data in php?

thinkphp queries the data of the day, this week, this month, and this year

The code is such as:

//当天时间
$where['time'] = array(
    array('egt',strtotime(date('Y-m-d',time())),
    array('lt',strtotime(date('Y-m-d',time())).'+1 day')
);
// 本周时间
$where['time'] = array(
    array('egt',strtotime(date('Y-m-d',time())).'-'.date('w',time()).' day'),
    array('lt',strtotime(date('Y-m-d',time())).'+1 week -'.date('w',time()).' day');
);
// 本月时间
$where['time'] = array(
    array('egt',strtotime(date('Y-m',time()))),
    array('lt',strtotime(date('Y-m',time()).'+1 month'))
);
// 本年时间
$where['time'] = array(
    array('egt',strtotime(date('Y',time()))),
    array('lt',strtotime(date('Y',time()).'+1 year'))
);

The above is the query condition, which is directly applied to the query The statement is enough

$result = $db->where($where)->select();

Correction of the query time of this year above

$where['time'] = array(
    array('egt',strtotime(date('Y-01-01',time())),
    array('lt',strtotime(date('Y-01-01',time()).'+1 year'))
);

Repaired version

//昨天时间
    $yesterday['addtime'] = array(
array('egt',strtotime(date('Y-m-d',time()).' -1 day')),
array('lt',strtotime(date('Y-m-d',time())))
);
//当天时间
$day['addtime'] = array(
array('egt',strtotime(date('Y-m-d',time()))),
array('lt',strtotime(date('Y-m-d',time()).' +1 day'))
);
// 本月时间
$month['addtime'] = array(
array('egt',strtotime(date('Y-m',time()))),
array('lt',strtotime(date('Y-m',time()).' +1 month'))
);
// 本周时间
$week['addtime'] = array(
array('egt',strtotime(date('Y-m-d',time()).'-'.date('w',time()).' day')),
array('lt',strtotime(date('Y-m-d',time()).'+1 week -'.date('w',time()).' day'))
);

Recommended learning: "PHP Video tutorial》《The latest 10 thinkphp video tutorials

The above is the detailed content of How to query this year's data in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn