zoukankan      html  css  js  c++  java
  • 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' )");

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

  • 相关阅读:
    HFish 源码Git下载 源码编译执行
    Windows注册表-学习总结
    利用PHPStudy搭建Xdebug调试环境
    Python3报错Crypto失败(from Crypto.Cipher import AES ModuleNotFoundError: No module named 'Crypto')
    Django后台管理admin字段控制显示长度(字段内容过长,省略号替代)
    PHP代码审计-小题一道
    golang编程-小问题
    迅雷影音播放器-ass字幕乱码-问题
    《独自等待》观影有感
    Python urllib URL 处理模块
  • 原文地址:https://www.cnblogs.com/sunny-blog/p/3897371.html
Copyright © 2011-2022 走看看