php 获取上周、上个月、下个月、本月的日期
1、获取一周前的日期
$time=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-7,date("Y")));
2、获取一个月前的日期
$time=date("Y-m-d",mktime(0,0,0,date("m")-1,date("d"),date("Y")));
3、获取当前时间
date("Y-m-d",time());
查询一天:
select * from table where to_days(column_time) = to_days(now());
select * from table where date(column_time) = curdate();
查询一周:
select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY)<=date(column_time);
查询一个月:
select * from table where DATE_SUB(CURDATE(), INTERVAL 1 MONTH)<=date(column_time);
简要说明:
to_days(date)给定一个日期, 返回一个天数 (从年份0开始的天数 );DATE_SUB(date,INTERVAL expr type)函数从日期减去指定的时间间隔。column_time参数表示每条记录对应的日期时间字段。
获取日期
date_default_timezone_set("UTC");
function GetTheMonth($date){//获取指定日期所在月的第一天和最后一天
$firstday = date("Y-m-01",strtotime($date));
$lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day"));
return array($firstday,$lastday);
}
function GetPurMonth($date){//获取指定日期上个月的第一天和最后一天
$time=strtotime($date);
$firstday=date('Y-m-01',strtotime(date('Y',$time).'-'.(date('m',$time)-1).'-01'));
$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
return array($firstday,$lastday);
}
function GetNextMonth($date){//获取指定日期下个月的第一天和最后一天
$arr = getdate();
if($arr['mon'] == 12){
$year = $arr['year'] +1;
$month = $arr['mon'] -11;
$day = $arr['mday'];
if($day < 10){
$mday = '0'.$day;
}else {
$mday = $day;
}
$firstday = $year.'-0'.$month.'-01';
$lastday = $year.'-0'.$month.'-'.$mday;
}else{
$time=strtotime($date);
$firstday=date('Y-m-01',strtotime(date('Y',$time).'-'.(date('m',$time)+1).'-01'));
$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
}
return array($firstday,$lastday);
}