php 与Mysql日期数据的常用统计函数

发表:2013-11-05 13:48:10

php 获取时间今天明天昨天时间戳

<?php
echo "今天:".date("Y-m-d")."<br>";   
echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";   
echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>";
echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>";   
echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";   
echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>";   
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>";   
echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>";   
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>";   
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";  
strtotime()函数的作用是将日期时间描述解析为 Unix 时间戳
int strtotime  ( string time [, int now] )
?>


常用的Mysql日期统计处理语句(被统计的字段add_time统一为UNINX时间戳格式)


1、统计今天

select count(*) from `z_goods` where add_time between UNIX_TIMESTAMP('2013-11-05 00:00:00') and UNIX_TIMESTAMP('2013-11-06 00:00:00');


2、统计昨天

$sql="SELECT count(*) as count FROM $table_name  WHERE to_days(now())  - to_days(date_format(FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%S'),'%Y-%m-%d')) <= 1";



3、统计当前月内

select count(*) from `z_goods` where date_format(FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%S'),'%Y-%m')=date_format(now(),'%Y-%m')


4、统计一个月内

$monthTime=$timer*30;
$Times=($monthTime*24*60*60);
//查找个月内的数据
$where.=" and (UNIX_TIMESTAMP()-add_time<={$Times} and ({$Times}+100)>UNIX_TIMESTAMP()-add_time) ";



5、统计上一个月内

$sql="select count(*) as count from `$table_name` where YEAR(FROM_UNIXTIME(add_time,'%y-%m-%d')) = YEAR(now()) and MONTH(FROM_UNIXTIME({$filed_name},'%y-%m-%d')) = MONTH(now()) ";


相关文章