首页 > 代码库 > PHP日期、时间戳相关的小程序

PHP日期、时间戳相关的小程序

1、日期区间内的日期列表(天):

1 public function dateExtent($begin,$end){2         $begin    =    strtotime($begin);3         $end    =    strtotime($end);4         while($begin<=$end){5             $dateArr[]    =    date(‘Y-m-d‘,$begin);6             $begin    +=    86400;    7         }8         return $dateArr;9     }

注释:

$begin  =  ‘2014-07-29‘;

$end  =  ‘2014-08-05‘;

返回:Array ( [0] => 2014-07-29 [1] => 2014-07-30 [2] => 2014-07-31 [3] => 2014-08-01 [4] => 2014-08-02 [5] => 2014-08-03 [6] => 2014-08-04 [7] => 2014-08-05 )

 

2、日期区间内的月份列表(月):

 1 public function monthExtent($begin,$end){ 2         $begin    =    strtotime($begin); 3         $end    =    strtotime($end); 4         $begin    =    date(‘Y-m‘,$begin); 5         $end    =    date(‘Y-m‘,$end); 6         $begin    =    strtotime($begin.‘-01‘); 7         $end    =    strtotime($end.‘-01‘); 8         while($begin<=$end){ 9             $monthArr[]    =    date(‘Y-m‘,$begin);10             $begin    +=    strtotime(‘+1 month‘,$begin)-$begin;11         }12         return $monthArr;13     }

注释:

$begin  =  ‘2013-10-07‘;

$end  =  ‘2014-02-05‘;

返回:Array ( [0] => 2013-10 [1] => 2013-11 [2] => 2013-12 [3] => 2014-01 [4] => 2014-02 )

 

3、指定日期的起始时间戳和结束时间戳:

1 $Tbegin    =    strtotime($date.‘ 00:00:00‘);2 $Tend    =    strtotime($date.‘ 23:59:59‘);

 

4、指定月份的起始时间戳和结束时间戳:

1 $Mbegin    =    strtotime($month.‘-01 00:00:00‘);2 $Mend    =    strtotime(date(‘Y-m-d‘,strtotime($month.‘-01 +1 month -1 day‘)).‘ 23:59:59‘);

 

另附:

数据库存储日期格式为时间戳;

PHP 统计查询每天的数量:

$Model->query("SELECT count( distinct did ) AS num, from_unixtime( `datetime` , ‘%Y-%m-%d‘ )AS time FROM `dealer_sell` WHERE uid=".$uid." and `datetime`>=".$begin." and `datetime` <=".$end." GROUP BY from_unixtime( `datetime` , ‘%Y%m%d‘ )");

注:没有的日期,显示为空。