$beginTime?=?mktime(0,0,0,date("m"),date("d")-1,date("y"));
echo?date("Y-m-d?H:i:s",$b)."-".date("Y-m-d?H:i:s",$e)."\r\n";
}
有没有其他办法我不知道,这个是我所知道的
可以使用mktime函数,详细方法如下:
$now?=?time();
$yesterday?=?$now?-?$secondsOneDay;
解释:
secondsOneDay为每天的秒数
now为当前的时间戳
mktime用于生成一个时间戳,参数分别为小时、分钟、秒、月、日、年
date函数取指定时间戳的某个日期参数,如某个时间戳的年份、月份等,其中n参数为月份、j参数为日期、Y参数为年份
echo?date("Y-m-d",$time);
/*今天*/
select?*?from?表名?where?to_days(时间字段)?=?to_days(now());
/*昨天*/
select?*?from?表名?where?to_days(now())-to_days(时间字段)?=?1;
/*查询当前这周的数据*/
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获取开始和结束时间
//当前时间
$start
=
strtotime(date('Y-m-d
H:i:s'));
//如果是1周后,则为$start
+
*
$long
//结束时间
$end
date('Y-m-d
H:i:s',
$long);
php可以用函数time()来获取Unix
时间戳,但是只能获取当前的,不能填入参数计算
通过php获取系统前一天的日期,用到的工具,notepad++,步骤如下:
通过notepad++写一个php函数另存为time.php文件.
说明:代码的含义,就是先通过strtotime将当前时间减去一天转化成unix时间戳,然后data函数是将unix时间戳转化成年月日时分秒.
注意事项:服务器需要支持php环境.