
正文
php查询当月的数据 php查询数据表
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何用PHP 获取今天之前,本周之前,本月之前,本年之前,今天,本周,本月,本年的数据呢
/*今天*/
select * from 表名 where to_days(时间字段) = to_days(now());
/*昨天*/
select * from 表名 where to_days(now())-to_days(时间字段) = 1;
/*近7天*/
select * from 表名 where date_sub(curdate(), interval 7 day) = date(时间字段);
/*查询距离当前现在6个月php查询当月的数据的数据*/
select * from 表名 where 时间字段 between date_sub(now(),interval 6 month) and now();
/*查询当前这周php查询当月的数据的数据*/
select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now());
/*查询上周php查询当月的数据的数据*/
select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now())-1;
/*查询当前月份php查询当月的数据的数据*/
select * from 表名 where date_format(时间字段,'%Y-%m')=date_format(now(),'%Y-%m');
/*查询上个月的数据*/
select * from 表名 where date_format(时间字段,'%Y-%m')=date_format(date_sub(curdate(), interval 1 month),'%Y-%m');
其它获取类似以上的代码显示
相关问答
Q1: thinkphp 怎么查本周 本月时间范围内的数据
求本周的开始和结束时间
$w = date('w',time()) - 1;
$start_time = time() - $w * 60 * 60 * 24; //星期一的时间戳
$end_time = time() + (6 - $w) * 60 * 60 * 24; //星期天的时间戳
M('tablename')-where("create_time = {$start_time} and create_time = $end_time")-select();
月份的也很简单了,求出本月开始和结束的时间,然后在根据时间查询就可以了
Q2: thinkphp中如何查询当天,本周的,本月的,本年的数据,
//当天时间
$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'))
);
上面是查询条件,直接运用到查询语句就可以了
$result = $db-where($where)-select();
更正下上面的那个 本年 查询时间
$where['time'] = array(
array('egt',strtotime(date('Y-01-01',time())),
array('lt',strtotime(date('Y-01-01',time()).'+1 year'))
);
Q3: php按当前年份、季度、当月,查询mysql数据库并输出数组
PHP查询到php查询当月的数据的数据存放到数组里面php查询当月的数据,一般使用$arr[]=$row的方式实现php查询当月的数据,$row是mysql_fetch_array获得的一行数据,本身是一个数组,执行上面的语句之后,这一行会添加存放在额为数组$arr的最后。 典型的例子代码是这样的php查询当月的数据:mysql_connect('127.0.0.1', 'root', '123456');$sql='select * from test.tab';if ($res=mysql_query($sql)){ while($row=mysql_fetch_array($res)) $result[]=$row; mysql_free_resule($res);}else echo "执行SQL语句:$sql\n错误php查询当月的数据:".mysql_error();echo '查询结果在下面的额为数组里面:';print_r($result);echo '';
php查询当月的数据的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php查询数据表、php查询当月的数据的信息别忘了在本站进行查找喔。






