求本周的开始和结束时间
创新互联公司专注于企业成都全网营销、网站重做改版、定边网站定制设计、自适应品牌网站建设、H5建站、商城网站制作、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为定边等各大城市提供网站开发制作服务。
$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();
月份的也很简单了,求出本月开始和结束的时间,然后在根据时间查询就可以了
/时间短查询
$starttime=GetMkTime($starttime);
$endtime=GetMkTime($endtime);
if($issettime==1){
if(!empty($starttime) !empty($endtime)){
$wheresql .=" AND paidan_time BETWEEN $starttime AND ".$endtime."";
}else if(!empty($starttime) empty($endtime)){
$endtime=time();
$wheresql .=" AND paidan_time BETWEEN $starttime AND ".$endtime."";
}else if(empty($starttime) !empty($endtime)){
$starttime=GetMkTime("2012-1-1");
$wheresql .=" AND paidan_time BETWEEN $starttime AND ".$endtime."";
}
}else if($issettime==2){
if(!empty($starttime) !empty($endtime)){
$wheresql .=" AND shousu_time BETWEEN $starttime AND ".$endtime."";
}else if(!empty($starttime) empty($endtime)){
$endtime=time();
$wheresql .=" AND shousu_time BETWEEN $starttime AND ".$endtime."";
}else if(empty($starttime) !empty($endtime)){
$starttime=GetMkTime("2012-1-1");
$$wheresql .=" AND shousu_time BETWEEN $starttime AND ".$endtime."";
}
}else
{
if(!empty($starttime)){
if($isset=="jr"){
$starttime=GetDateMK($starttime);
$wheresql .=" AND datediff('".$starttime."',FROM_UNIXTIME(`paidan_time`))=0 ";
}else{
//$endtime=time();
$wheresql .=" AND paidan_time BETWEEN $starttime AND ".$endtime."";
}
/*$nowtime = time();
$passtime=$nowtime-$starttime;
$tianshu=floor($passtime/(24*60*60));
echo floor($passtime/(24*60*60)); //天数
//echo 'br/';
$starttime = $nowtime - ($tianshu*24*3600);
$wheresql .= " AND paidan_time='$starttime' ";*/
}
}
/*今天*/
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');
其它获取类似以上的代码显示
function aweek($gdate = \"\", $first = 0){if(!$gdate) $gdate = date(\"Y-m-d\");$w = date(\"w\", strtotime($gdate));//取得一周的第几天,星期天开始0-6$dn = $w ? $w - $first : 6;//要减去的天数$st = date(\"Y-m-d\", strtotime(\"$gdate -\".$dn.\" days\"));$en = date(\"Y-m-d\", strtotime(\"$st +6 days\"));return array($st, $en);//返回开始和结束日期}$weeks=implode(\"\' and \'\", aweek(\"\", 1));mysql_query(\"select * from table??where date Between \'$weeks\'\" )
echo date('Y-m-d',strtotime ( "last Monday -1 week" )), "\n" ;
echo date('Y-m-d',strtotime ( "last Sunday" )), "\n" ;
通过这个函数获取上个周的开始和结束,然后sql查询
可以研究下这个函数strtotime 。可以满足你的需求
思路:先关联数据库,然后执行sql语句,最后返回结果!
需要关联上数据库
?php
//创建对象并打开连接,最后一个参数是选择的数据库名称
$mysqli = new mysqli('localhost','root','','volunteer');
//检查连接是否成功
if (mysqli_connect_errno()){
//注意mysqli_connect_error()新特性
die('Unable to connect!'). mysqli_connect_error();
}
$sql = "SELECT *FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) = date(时间字段名)";
//执行sql语句,完全面向对象的
$result = $mysqli-query($sql);
while($row = $result-fetch_array()){
echo $row[0];
}
?
执行一条sql语句
SELECT *FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) = date(时间字段名)
3.运行结果、返回结果集:
$result=mysql_query($query);
并对结果进行处理!