1.strtotime()函数
//date('n') 第几个月
//date("w") 本周周几
//date("t") 本月天数
2.时间段:
//当前时间
echo date("Y-m-d H:i:s",time());
//明天此时时间
echo date("Y-m-d H:i:s",strtotime("+1 day"));
//指定时间
echo date("Y-m-d H:i:s",strtotime("-1 day"));
//下星期时间
echo date("Y-m-d H:i:s",strtotime("+1 week"))
//上个星期此时时间
echo date("Y-m-d H:i:s",strtotime("-1 week"));
//下星期几时间
echo date("Y-m-d H:i:s",strtotime("next Thursday"));
//指定时间
echo date("Y-m-d H:i:s",strtotime("last Thursday"));
今天
$startDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
$endDate = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
昨天
$startDate = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
$endDate = mktime(0,0,0,date("m"),date("d"),date("Y"))-1;
明天
$startDate = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
$endDate = mktime(0,0,0,date("m"),date("d")+2,date("Y"))-1;
本周
$w = date("w");
$startDate = mktime(0,0,0,date("m"),date("d")-$w+1,date("Y"));
$endDate=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
上周
$startDate=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
$endDate=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
下周
$startDate=mktime(0,0,0,date('m'),date('d')-date('w')+7+1,date('Y'));
$endDate=mktime(23,59,59,date('m'),date('d')-date('w')+7+7,date('Y'));
本月
$startDate = mktime(0,0,0,date("m"),1,date("Y"));
$endDate = mktime(23,59,59,date('m'),date('t'),date('Y'))-1;
上月
$startDate = mktime(0,0,0,date("m")-1,1,date("Y"));
$endDate = mktime(0,0,0,date("m"),1,date("Y"))-1;
下月
$startDate = mktime(0,0,0,date("m")+1,1,date("Y"));
$endDate = mktime(0,0,0,date("m")+2,1,date("Y"))-1;
本季度
$getMonthDays = date("t",mktime(0, 0 , 0,date('n')+(date('n')-1)%3,1,date("Y")));//本季度未最后一月天数
echo '<br>本季度:<br>';
echo date('Y-m-d H:i:s', mktime(0, 0, 0,date('n')-(date('n')-1)%3,1,date('Y'))),"\n";
echo date('Y-m-d H:i:s', mktime(23,59,59,date('n')+(date('n')-1)%3,$getMonthDays,date('Y'))),"\n";
$season = ceil((date('n'))/3);//当月是第几季度
echo '<br>本季度起始时间:<br>';
echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n";
echo date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'))),"\n";
$season = ceil((date('n'))/3)-1;//上季度是第几季度
echo '<br>上季度起始时间:<br>';
echo date('Y-m-d H:i:s', mktime(0, 0, 0,$season*3-3+1,1,date('Y'))),"\n";
echo date('Y-m-d H:i:s', mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y'))),"\n";
今年
$startDate = mktime(0,0,0,1,1,date("Y"));
$endDate = mktime(0,0,0,1,1,date("Y")+1)-1;
按年汇总,统计:
select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y');
按月汇总,统计:
select sum(mymoney) as totalmoney, count(*) as sheets from mytable group by date_format(col, '%Y-%m');
按季度汇总,统计:
select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3));
select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by concat(date_format(col, '%Y'),FLOOR((date_format(col, '%m')+2)/3));
按小时:
select sum(mymoney) as totalmoney,count(*) as sheets from mytable group by date_format(col, '%Y-%m-%d %H ');
查询 本年度的数据:
SELECT * FROM mytable WHERE year(FROM_UNIXTIME(my_time)) = year(curdate())
查询数据附带季度数:
SELECT id, quarter(FROM_UNIXTIME(my_time)) FROM mytable;
查询 本季度的数据:
SELECT * FROM mytable WHERE quarter(FROM_UNIXTIME(my_time)) = quarter(curdate());
本月统计:
select * from mytable where month(my_time1) = month(curdate()) and year(my_time2) = year(curdate())
本周统计:
select * from mytable where month(my_time1) = month(curdate()) and week(my_time2) = week(curdate())
N天内记录:
WHERE TO_DAYS(NOW())-TO_DAYS(时间字段)<=N