php获取每周的数据 php获取本周一到周日

PHP查找一周内的数据 和一段范围的数据 怎么写SQL-CSDN论坛/时间短查询
$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' ";*/
}
}
如何用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');
其它获取类似以上的代码显示
php+mysql怎么查询本周的数据思路:先关联数据库,然后执行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);
并对结果进行处理!
php如何统计本星期的数据? - 技术问答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\'\" )

推荐阅读