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' )");
注:沒有的日期,顯示為空。
轉載于:https://www.cnblogs.com/sunny-blog/p/3897371.html
總結
以上是生活随笔為你收集整理的PHP日期、时间戳相关的小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络编程知识预备(1) ——了解OSI网
- 下一篇: PHP扩展开发(3)-config.m4