求本周的開始和結(jié)束時間
創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、定邊網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5建站、商城網(wǎng)站制作、集團公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為定邊等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
$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();
月份的也很簡單了,求出本月開始和結(jié)束的時間,然后在根據(jù)時間查詢就可以了
/時間短查詢
$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));??//天數(shù)
//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(時間字段);
/*查詢距離當(dāng)前現(xiàn)在6個月的數(shù)據(jù)*/
select?*?from?表名?where?時間字段?between?date_sub(now(),interval?6?month)?and?now();
/*查詢當(dāng)前這周的數(shù)據(jù)*/
select?*?from?表名?where?yearweek(date_format(時間字段,'%Y-%m-%d'))?=?yearweek(now());
/*查詢上周的數(shù)據(jù)*/
select?*?from?表名?where?yearweek(date_format(時間字段,'%Y-%m-%d'))?=?yearweek(now())-1;
/*查詢當(dāng)前月份的數(shù)據(jù)*/
select?*?from?表名?where?date_format(時間字段,'%Y-%m')=date_format(now(),'%Y-%m');
/*查詢上個月的數(shù)據(jù)*/
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;//要減去的天數(shù)$st = date(\"Y-m-d\", strtotime(\"$gdate -\".$dn.\" days\"));$en = date(\"Y-m-d\", strtotime(\"$st +6 days\"));return array($st, $en);//返回開始和結(jié)束日期}$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" ;
通過這個函數(shù)獲取上個周的開始和結(jié)束,然后sql查詢
可以研究下這個函數(shù)strtotime ??梢詽M足你的需求
思路:先關(guān)聯(lián)數(shù)據(jù)庫,然后執(zhí)行sql語句,最后返回結(jié)果!
需要關(guān)聯(lián)上數(shù)據(jù)庫
?php
//創(chuàng)建對象并打開連接,最后一個參數(shù)是選擇的數(shù)據(jù)庫名稱
$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(時間字段名)";
//執(zhí)行sql語句,完全面向?qū)ο蟮?/p>
$result?=?$mysqli-query($sql);
while($row?=?$result-fetch_array()){
echo?$row[0];
}
?
執(zhí)行一條sql語句
SELECT?*FROM?表名?where?DATE_SUB(CURDATE(),?INTERVAL?7?DAY)?=?date(時間字段名)
3.運行結(jié)果、返回結(jié)果集:
$result=mysql_query($query);
并對結(jié)果進行處理!