
正文
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个月的数据*/
select * from 表名 where 时间字段 between date_sub(now(),interval 6 month) and now();
/*查询当前这周的数据*/
select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now());
/*查询上周的数据*/
select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now())-1;
/*查询当前月份的数据*/
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中如何查询当天,本周的,本月的,本年的数据,
//当天时间
$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'))
);
Q2: php怎样获取当前月的所有日期,放在一个数组里面?
$j = date(j); //获取当前月份天数
$start_time = strtotime(date('Y-m-01')); //获取本月第一天时间戳
$array = array();
for($i=0;$i$j;$i++){
$array[] = date('Y-m-d',$start_time+$i*86400); //每隔一天赋值给数组
}
print_r($array);
Q3: php中如何实现按月份查询数据库中的信息?
试试这个
select * from table1 where month(date)='您要查询的月份' order by date;
year(date) 即为年份
day(date) 顾名思义
Q4: 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();
月份的也很简单php获取本月数据了php获取本月数据,求出本月开始和结束的时间php获取本月数据,然后在根据时间查询就可以php获取本月数据了
Q5: php中用time()函数存入时间,如何查询当月的数据
这个time()函数是将时间保存成时间戳格式,则要查当月数据,只要查当月第一天到当月最后一天的之间的数据即可。
假设这个用来判断的字段是date
sql语句
SELECT ………… WHERE………… `date` = 本月第一天的time值 AND `date` 下个月第一天的time值
所以这里就只要获取当月第一天以及下个月第一天的时间戳
具体如下:
?php
$cur = date('Y-m',time());//当天年月
$cur_y = date('Y',time());//当天年份
$cur_m = date('m',time());//当天月份
$cur_f = $cur . '-1';//本月首日
$first = strtotime($cur_f);//时间戳最小值,本月第一天时间戳
//下月首日
if($cur_m=12){
$cur_n = ($cur_y+1) . '-1-1';
}else{
$cur_n = $cur_y . '-' . ($cur_m+1) . '-1';
}
$last = strtotime($cur_n);//时间戳最大值,下个月第一天时间戳
?
再把$first 和 $last 放入sql语句里面就可以查询到数据了
关于php获取本月数据和php获取当前时间提交数据库的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。





