zoukankan      html  css  js  c++  java
  • php中关于时间的用法

    一、时间戳相关:

           当前时间戳:time();
     
          把时间戳转换为时间显示:date("Y-m-d H:i:s", $a);
     
          把日期时间转换为时间戳:strtotime($a);
     
          获取当前日期时间戳:strtotime(date("Y-m-d"));
     
         判断是否为日期格式:is_date($a);
     
         判断出生日期是否合法:strtotime($a) > strtotime(date("Y-m-d"))则不合法;
     
         日期时间拼接:date("Y-m-d", $date)." ".$begintime."~".$endtime;
      
       把不规则的日期字符串转换成规则的字符串再转成整形数据:intval(date("Ymd",strtotime("2016-1-9")))
     
      
    date_default_timezone_set('Asia/Chongqing');
    echo date('Y-m-d H:i:s');
    
    date_default_timezone_set(’PRC’) ;
    date_default_timezone_get();   //PRC
    

    二、与时间有关的一些方法:

    d  月份中的第几天,有前导零的 2 位数字 01 到 31 
    
    D  星期中的第几天,文本表示,3 个字母 Mon 到 Sun 
    
    j  月份中的第几天,没有前导零 1 到 31 
    
    l  (“L”的小写字母) 星期几,完整的文本格式 Sunday 到 Saturday 
    
    N  ISO-8601 格式数字表示的星期中的第几天(PHP 5.1.0 新加) 1(星期一)到 7(星期天) 
    
    S  每月天数后面的英文后缀,2 个字符 st,nd,rd 或者 th。可以和 j 一起用 
    
    w  星期中的第几天,数字表示 0(星期天)到 6(星期六) 
    
    z  年份中的第几天 0 到 366 
    
    W  ISO-8601 格式年份中的第几周,每周从星期一开始(PHP 4.1.0 新加的) 42(当年的第 42 周) 
    
    F  月份,完整的文本格式,例如 January 或者 March January 到 December 
    
    m  数字表示的月份,有前导零 01 到 12 
    
    M  三个字母缩写表示的月份 Jan 到 Dec 
    
    n  数字表示的月份,没有前导零 1 到 12 
    
    t  给定月份所应有的天数 28 到 31 
    
    L  是否为闰年 如果是闰年为 1,否则为 0 
    
    o  ISO-8601 格式年份数字。
    
    Y  4 位数字完整表示的年份 例如:1999 或 2003 
    
    y  2 位数字表示的年份 例如:99 或 03 
    
    a  小写的上午和下午值 am 或 pm 
    
    A  大写的上午和下午值 AM 或 PM 
    
    B  Swatch Internet 标准时 000 到 999 
    
    g  小时,12 小时格式,没有前导零 1 到 12 
    
    G  小时,24 小时格式,没有前导零 0 到 23 
    
    h  小时,12 小时格式,有前导零 01 到 12 
    
    H  小时,24 小时格式,有前导零 00 到 23 
    
    i  有前导零的分钟数 00 到 59> 
    
    s  秒数,有前导零 00 到 59> 
    
    e  时区标识(PHP 5.1.0 新加) 例如:UTC,GMT,Atlantic/Azores 
    
    I  是否为夏令时 如果是夏令时为 1,否则为 0 
    
    O  与格林威治时间相差的小时数 例如:+0200 
    
    P  与格林威治时间(GMT)的差别,小时和分钟之间有冒号分隔 例如:+02:00 
    
    T  本机所在的时区  
    
    Z  时差偏移量的秒数。UTC 西边的时区偏移量总是负的,UTC 东边是正的。 -43200 到 43200 
    
    c  ISO 8601 格式的日期(PHP 5 新加) 2004-02-12T15:19:21+00:00 
    
    r  RFC 822 格式的日期 例如:Thu, 21 Dec 2000 16:01:07 +0200 
    
    U  从 Unix 纪元(January 1 1970 00:00:00 GMT)开始至今的秒数 time()获得时间戳 
    
    1.  时间范围
      $y=date("Y",time());     
      $m=date("m",time());     
      $d=date("d",time());    
      $start_time = mktime(9, 0, 0, $m, $d ,$y);     
      $end_time = mktime(19, 0, 0, $m, $d ,$y);     
      $time = time();     
      if($time >= $start_time && $time <= $end_time)     
      {       
            // do something....     
      }

        

          2. 根据出生年月计算年龄

        

           /**
            * 根据出生年月计算年龄
           * @param    data $birthday 用户出生年月
           * @return     int 返回年龄
           */
          public static function age($birthday)
          {
              $age = date('Y', time()) - date('Y', strtotime($birthday)) - 1;
              if (date('m', time()) == date('m', strtotime($birthday)))
              {
                  if (date('d', time()) > date('d', strtotime($birthday)))
                  {
                      $age++;
                  }
              }
              elseif (date('m', time()) > date('m', strtotime($birthday)))
              {
                  $age++;
              }
     
              $age = $age < 0 ? 0 : $age;
     
              return $age;
          }
        
     
    function birthday($birthday){ 
      $age = strtotime($birthday); 
      if($age === false){ 
        return false; 
      } 
      list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age)); 
      $now = strtotime("now"); 
      list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now)); 
      $age = $y2 - $y1; 
      if((int)($m2.$d2) < (int)($m1.$d1)) 
        $age -= 1; 
      return $age; 
    } 
    echo birthday('1986-07-22'); 
    
       
            3. 换算成字符串
     
                    $hour = intval($a / 3600);
                    $minute = intval(($a - 3600 * $hour) / 60);
                    $second = intval($a - $hour * 3600 - $minute * 60);
                    $return_time = $hour . "时" . $minute . "分" . $second . "秒";
     
                   $minutes = round($time_sum/60);
                    if ($minutes >= 60){
                            $hour = floor($minutes/60);
                            $minutes = $minutes%60;
                            $time_res = $hour.' 小时 ';
                            $minutes != 0  &&  $time_res .= $minutes.' 分';
                     }else{
                           $time_res = $minutes.'分钟';
                    }​
            4. 用php获取本周、本月第一天与最后天时间戳
            
                获取今天的时间范围:
                     
    $start = mktime(0,0,0,date("m"),date("d"),date("Y")); 
    $end = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
    

         获取本周第一天/最后一天的时间戳 

    /**
         * 获取本周第一天/最后一天的时间戳
         * @author		chenlu <chenlu@spapa.com.cn>
         * @date		2015-04-21
         */
        public static function getWeekTime()
        {
        	$year = date("Y");
            $month = date("m");
            $day = date('w');
            $nowMonthDay = date("t");
    
            $firstday = date('d') - $day;
            if(substr($firstday,0,1) == "-"){
             $firstMonth = $month - 1;
             $lastMonthDay = date("t",$firstMonth);
             $firstday = $lastMonthDay - substr($firstday,1);
             $time_1 = strtotime($year."-".$firstMonth."-".$firstday);
            }else{
             $time_1 = strtotime($year."-".$month."-".$firstday);
            }
    
            $lastday = date('d') + (7 - $day);
            if($lastday > $nowMonthDay){
             $lastday = $lastday - $nowMonthDay;
             $lastMonth = $month + 1;
             $time_2 = strtotime($year."-".$lastMonth."-".$lastday);
            }else{
             $time_2 = strtotime($year."-".$month."-".$lastday);
            }
            return array(
                'start_time' => $time_1,
                'end_time' => $time_2
            );
        	
        }
    

             获取本月第一天/最后一天的时间戳

    $year = date("Y");
    $month = date("m");
    $allday = date("t");
    $strat_time = strtotime($year."-".$month."-1");
    $end_time = strtotime($year."-".$month."-".$allday);
    

    5.php的date()函数判断今天是星期几

    $weekarray=array("日","一","二","三","四","五","六");
    echo "星期".$weekarray[date("w")];
    

     或者

    function getWeek(){
    	$week = date("w");
    	switch($week){
    		case 1:
    			return "星期一";
    			break;
    		case 2:
    			return "星期二";
    			break;
    		case 3:
    			return "星期三";
    			break;
    		case 4:
    			return "星期四";
    			break;
    		case 5:
    			return "星期五";
    			break;
    		case 6:
    			return "星期六";
    			break;
    		case 0:
    			return "星期日";
    			break;
    	}
    }
    echo "今天是:".getWeek();
    

     判断指定日期是周几

    date("w",strtotime("2013-01-14"));
    

    6.php 判断今天的前一天,或前后多少天的代码

    <?php    
    date_default_timezone_set('PRC'); //默认时区    
    echo "今天:",date("Y-m-d",time()),"<br>";    
    echo "今天:",date("Y-m-d",strtotime("18 june 2008")),"<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>";    
    ?>
    

     php判断一个日期距今天还有多少天

    echo (strtotime('2020-5-20′)-strtotime(date(“Y-m-d”)))/86400; 
    
           /**
            * 获取日期星期列表
            */
            public static function getWeekDay()
            {
                $time_date0 = date("Y-m-d"); 
                $time_date = strtotime($time_date0);
                $range = 7;
                $week_day_list = array();
                $weekarray=array(7,1,2,3,4,5,6);
                for ($index = 0; $index < $range; $index++) {
                    $date_time = $time_date + 86400 * $index;
                    $week_day_list0 = array();
                    $week_day_list0['date'] = date("Y-m-d",$date_time);
                    $week_day_list0['weeks'] = $weekarray[date("w",$date_time)]; 
                    $week_day_list0['weeks_str'] =     StadiumSku::getWeekStr($weekarray[date("w",$date_time)]); 
                    $week_day_list[] = $week_day_list0;
                }
                return $week_day_list;
            }
    

    7.php获取本周起始时间
     
    $start = date("Y-m-d H:i:s",mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y")));
    $end = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y")));
    是星期一到星期天的
    
    $w=date('w',$_SERVER['REQUEST_TIME']);
    $start_time = strtotime('today -'.($w?($w - 1):6).' day');
    $start_day = Date('Y-m-d H:i:s',$start_time);
    $end_day = Date('Y-m-d H:i:s',$start_time + 604799);
    
     $time = time();
    $w_day=date("w",$time);
    if($w_day=='1'){
    $cflag = '+0';
    $lflag = '-1';
    }else{
    $cflag = '-1';
    $lflag = '-2';
    }
    $weekstar = strtotime(date('Y-m-d',strtotime("$cflag week Monday", $time))); //本周一零点的时间戳
    $weekend = strtotime(date('Y-m-d',strtotime("$cflag week Monday", $time)))+7*24*3600;//本周末零点的时间戳
    

    8.时间范围

    $t           = time();
     $timeSilce[] = array('start_date' => date('Y-m-d', $t - (3600 * 24 * 30)), 'end_date' => date('Y-m-d', $t));
     $timeSilce[] = array('start_date' => date('Y-m-d', $t - (3600 * 24 * 30 * 3)), 'end_date' => date('Y-m-d', $t));
     $timeSilce[] = array('start_date' => date('Y-m-d', $t - (3600 * 24 * 30 * 6)), 'end_date' => date('Y-m-d', $t));
    $timeSilce[] = array('start_date' => 0, 'end_date' => date('Y-m-d', $t - (3600 * 24 * 30 * 6)));
    

     9.上月起始日期

    $m = date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))); //上个月的开始日期
    $mo = date('t',strtotime($m)); //上个月共多少天
    

     10.今天、昨天、本周、上周、本月、上月的时间起始

    $t = time();
    $timeSilce[] = array('start_date' => date('Y-m-d', $t), 'end_date' => date('Y-m-d', $t));    //今天
    $timeSilce[] = array('start_date' => date('Y-m-d', $t- (3600 * 24)), 'end_date' => date('Y-m-d', $t- (3600 * 24)));    //昨天
    $timeSilce[] = array('start_date' => date("Y-m-d",mktime(0,0,0,date("m"),date("d")-date("w")+1,date("Y"))), 'end_date' => date("Y-m-d",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))));    //本周
    $timeSilce[] = array('start_date' => date("Y-m-d",mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'))), 'end_date' => date("Y-m-d",mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'))));    //上周
    $timeSilce[] = array('start_date' => date("Y-m-d",mktime(0,0,0,date('m'),1,date('Y'))), 'end_date' => date("Y-m-d",mktime(23,59,59,date('m'),date('t',strtotime(date('Y-m-d', mktime(0,0,0,date('m'),1,date('Y'))))),date('Y'))));    //本月
    $timeSilce[] = array('start_date' => date("Y-m-d",mktime(0,0,0,date('m')-1,1,date('Y'))), 'end_date' => date("Y-m-d",mktime(23,59,59,date('m')-1,date('t',strtotime(date('Y-m-d', mktime(0,0,0,date('m')-1,1,date('Y'))))),date('Y'))));    //上月
    print_r($timeSilce);exit;
    
    Array
    (
        [0] => Array
            (
                [start_date] => 2015-09-15
                [end_date] => 2015-09-15
            )
    
        [1] => Array
            (
                [start_date] => 2015-09-14
                [end_date] => 2015-09-14
            )
    
        [2] => Array
            (
                [start_date] => 2015-09-14
                [end_date] => 2015-09-20
            )
    
        [3] => Array
            (
                [start_date] => 2015-09-07
                [end_date] => 2015-09-13
            )
    
        [4] => Array
            (
                [start_date] => 2015-09-01
                [end_date] => 2015-09-30
            )
    
        [5] => Array
            (
                [start_date] => 2015-08-01
                [end_date] => 2015-08-31
            )
    
    )
    
     
    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 //author:zhxia 获取指定日期所在星期的开始时间与结束时间
    function getWeekRange($date){
        $ret=array();
        $timestamp=strtotime($date);
        $w=strftime('%u',$timestamp);
        $ret['sdate']=date('Y-m-d 00:00:00',$timestamp-($w-1)*86400);
        $ret['edate']=date('Y-m-d 23:59:59',$timestamp+(7-$w)*86400);
        return $ret;
    }
    
    //author:zhxia 获取指定日期所在月的开始日期与结束日期
    function getMonthRange($date){
        $ret=array();
        $timestamp=strtotime($date);
        $mdays=date('t',$timestamp);
        $ret['sdate']=date('Y-m-1 00:00:00',$timestamp);
        $ret['edate']=date('Y-m-'.$mdays.' 23:59:59',$timestamp);
        return $ret;
    }
    
    
    //author:zhxia  以上两个函数的应用
    function getFilter($n){
        $ret=array();
        switch($n){
            case 1:// 昨天
                $ret['sdate']=date('Y-m-d 00:00:00',strtotime('-1 day'));
                $ret['edate']=date('Y-m-d 23:59:59',strtotime('-1 day'));
            break;
            case 2://本星期
                $ret=getWeekRange(date('Y-m-d'));
            break;
            case 3://上一个星期
                $strDate=date('Y-m-d',strtotime('-1 week'));
                $ret=getWeekRange($strDate);
            break;
            case 4: //上上星期
                $strDate=date('Y-m-d',strtotime('-2 week'));
                $ret=getWeekRange($strDate);
            break;
            case 5: //本月
                $ret=getMonthRange(date('Y-m-d'));
                break;
            case 6://上月
                $strDate=date('Y-m-d',strtotime('-1 month'));
                $ret=getMonthRange($strDate);
            break;
        }
        return $ret;
    }
    

    php 获取一个月第一天与最后一天的代码

    function getthemonth($date)
    {
              $firstday = date('Y-m-01', strtotime($date));
              $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
              return array($firstday, $lastday);
    } 
    
                    $year = date("Y");
                    $month = date("m");
                    $allday = date("t");
                    $strat_time = strtotime($year."-".$month."-1");
                    $end_time = strtotime($year."-".$month."-".$allday);
                    echo date("Y-m-d", $strat_time);
                    echo "-----";
                    echo date("Y-m-d", $end_time);
                    echo "-----";
                    echo date('Y-m-01', strtotime('-2 month'));
                    echo "<br/>";
                    echo date('Y-m-t', strtotime('-2 month'));
                    echo "<br/>";
    
    var aa = new Date();
    alert(aa.toLocaleDateString());  //2016年1月20日
    alert(aa.toLocaleTimeString());  //上午11:10:20
    

    php获取当月天数及当月第一天及最后一天、上月第一天及最后一天实现方法: http://www.jxbh.cn/newshow.asp?id=1635&tag=2

     11.时间插件

    <label>指定时间段</label>
                    <input id="time_start" type="text" placeholder="2016-02-01" name="time_start" value="" style=" 150px;height:24px;"  onfocus="WdatePicker({skin:'whyGreen',startDate:'%y-%M-%d',dateFmt:'yyyy-MM-dd'})" class="Wdate" />至
                    <input id="time_end" type="text" placeholder="2016-02-01" name="time_end" value="" style=" 150px;height:24px;"  onfocus="WdatePicker({skin:'whyGreen',startDate:'%y-%M-%d',dateFmt:'yyyy-MM-dd'})" class="Wdate" />
    

     WdatePicker默认当前日期

    当日期框无论是何值,始终使用 1980-05-01 做为起始日期
    <input type="text" id="d222" onFocus="WdatePicker({startDate:%y-%M-%d,alwaysUseStartDate:true})"/>
    
    动态变量表
    %y  当前年
    
    %M  当前月
    
    %d   当前日
    
    %ld  本月最后一天
    
    %H   当前时
    
    %m  当前分
    
    %s   当前秒
    
    #{}   运算表达式,如:#{%d+1}:表示明天
    
    #F{}  {}之间是函数可写自定义JS代码
    

     12. php获取当前月月初至月末的时间戳,上个月月初至月末的时间戳

    当前月

    <?php
    $thismonth = date('m');
    $thisyear  = date('Y');
    $startDay = $thisyear . '-' . $thismonth . '-1';
    $endDay   = $thisyear . '-' . $thismonth . '-' . date('t', strtotime($startDay));
    $b_time       = strtotime($startDay);
    $e_time       = strtotime($endDay);
    

     上一月 

    <?php
    $thismonth = date('m');
    $thisyear  = date('Y');
    if ($thismonth == 1) {
        $lastmonth = 12;
        $lastyear  = $thisyear - 1;
    } else {
        $lastmonth = $thismonth - 1;
        $lastyear  = $thisyear;
    }
    $lastStartDay = $lastyear . '-' . $lastmonth . '-1';
    $lastEndDay   = $lastyear . '-' . $lastmonth . '-' . date('t', strtotime($lastStartDay));
    $b_time = strtotime($lastStartDay);
    $e_time = strtotime($lastEndDay);
    

     给定年份月份,获取最近几个月的月初、月末时间范围

           /**
             * @param $year 给定的年份
             * @param $month 给定的月份
             * @param $legth 筛选的区间长度 取前六个月就输入6
             * @param int $page 分页
             * @return array
             */
            function getLastTimeArea($year, $month, $legth, $page = 1)
            {
                    if (!$page)
                    {
                            $page = 1;
                    }
                    $monthNum = $month + $legth - $page * $legth;
                    $num = 1;
                    if ($monthNum < -12)
                    {
                            $num = ceil($monthNum / (-12));
                    }
                    $timeAreaList = [];
                    for ($i = 0; $i < $legth; $i++)
                    {
                            $temMonth = $monthNum - $i;
                            $temYear = $year;
                            if ($temMonth <= 0)
                            {
                                    $temYear = $year - $num;
                                    $temMonth = $temMonth + 12 * $num;
                                    if ($temMonth <= 0)
                                    {
                                            $temMonth += 12;
                                            $temYear -= 1;
                                    }
                            }
                            $temMonth = str_pad($temMonth, 2, '0', STR_PAD_LEFT);
                            //$startMonth = strtotime($temYear . '-' . $temMonth . '-01');//该月的月初时间戳
                            //$endMonth = strtotime($temYear . '-' . ($temMonth + 1) . '-01') - 86400;//该月的月末时间戳
    
                            $startMonth = strtotime($temYear . '-' . $temMonth . '-01');
                            $endMonth   = strtotime($temYear . '-' . $temMonth . '-' . date('t', $startMonth));
                            $res['startMonth'] = $temYear . '-' . $temMonth . '-01'; //该月的月初格式化时间
                            $res['endMonth'] = date('Y-m-d', $endMonth);//该月的月末格式化时间
                            $res['timeArea'] = implode(',', [$startMonth, $endMonth]);//区间时间戳
                            $timeAreaList[] = $res;
                    }
                    return array_reverse($timeAreaList);
                    //return $timeAreaList;
            }    
    

    13. PHP指定时间戳/日期加一天,一年,一周,一月

       

        <?php
        echo date('Y-m-d H:i:s',strtotime('now'));//当前时间戳 2017-01-09 21:04:11
        echo date('Y-m-d H:i:s',strtotime('+1second'));//当前时间戳+1秒 2017-01-09 21:04:12
        echo date('Y-m-d H:i:s',strtotime('+1minute'));//当前时间戳+1分 2017-01-09 21:05:11
        echo date('Y-m-d H:i:s',strtotime('+1hour'));//当前时间戳+1小时 2017-01-09 22:04:11
        echo date('Y-m-d H:i:s',strtotime('+1day'));//当前时间戳+1天 2017-01-10 21:04:11
        echo date('Y-m-d H:i:s',strtotime('+1week'));//当前时间戳+1周 2017-01-16 21:04:11
        echo date('Y-m-d H:i:s',strtotime('+1month'));//当前时间戳+1月 2017-02-09 21:04:11
        echo date('Y-m-d H:i:s',strtotime('+1year'));//当前时间戳+1年 2018-01-09 21:04:11
        echo date('Y-m-d H:i:s',strtotime('+12year 12month 12day 12hour 12minute 12second'));//当前时间戳+12年,12月,12天,12小时,12分,12秒 2030-01-22 09:16:23
        $t=1483967416;//指定时间戳
        echo $dt=date('Y-m-d H:i:s',$t);//2017-01-09 21:10:16
        /*方法一*/
        echo date('Y-m-d H:i:s',$t+1*24*60*60);//指定时间戳+1天 2017-01-10 21:10:16
        echo date('Y-m-d H:i:s',$t+365*24*60*60);//指定时间戳+1年 2018-01-09 21:10:16
        /*方法二*/
        //$dt是指定时间戳格式化后的日期
        echo date('Y-m-d H:i:s',strtotime("$dt+1day"));//指定时间戳+1天 2017-01-10 21:10:16
        echo date('Y-m-d H:i:s',strtotime("$dt+1year"));//指定时间戳+1年 2018-01-09 21:10:16
        /*方法三*/
        //$t是指定时间戳
        echo date('Y-m-d H:i:s',strtotime("+1day",$t));//指定时间戳+1天 2017-01-10 21:10:16
        echo date('Y-m-d H:i:s',strtotime("+1year",$t));//指定时间戳+1年 2018-01-09 21:10:16
        //指定时间戳加1月、1周、1小时、1分、1秒原理同上;
    

     14. 获取一星期的开始和结束日期

            /**
             * 获取一星期的开始和结束日期
             * @param int $weekStep 取值...-3、-2、-1、0、1、2、3...
             * @return array
             */
            function getWeekStartEnd($weekStep=0)
            {
                    //当前日期
                    $sdefaultDate = date("Y-m-d");
                    //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
                    $first=1;
                    //获取当前周的第几天 周日是 0 周一到周六是 1 - 6
                    $w=date('w',strtotime($sdefaultDate));
                    //获取本周开始日期,如果$w是0,则表示周日,减去 6 天
                    $week_start=date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days'));
                    //本周结束日期
                    $week_end=date('Y-m-d',strtotime("$week_start +6 days"));
                    $rs = array();
                    if($weekStep != 0)
                    {
                            $week_start = date('Y-m-d',strtotime("$week_start +" . $weekStep . " week"));
                            $week_end = date('Y-m-d',strtotime("$week_end +" . $weekStep . " week"));
                    }
                    $rs['week_start'] = $week_start;
                    $rs['week_end'] = $week_end;
                    return $rs;
            }    
    

     函数调用:

                    $weekStartEnd = getWeekStartEnd($week_step);
                    //print_r($weekStartEnd);exit;
                    $start_date = $weekStartEnd['week_start'];
                    $end_date = $weekStartEnd['week_end'];
                    $dateMap = array();
                    for($i = $start_date; $i <= $end_date; $i = date('Y-m-d', strtotime("$i +1 day")))
                    {
                            $temp = array();
                            $temp['date_str'] = $i;
                            $temp['date_int'] = intval(str_replace('-','',$i));
                            $temp['date_str2'] = substr($i, 5, 2) . '月' . substr($i, 8, 2) . '号';
                            $temp['weeks'] = date("w", strtotime($i));
                            $temp['weeks_str'] = StadiumSku::getWeekStr(date("w", strtotime($i)));
                            $dateMap[] = $temp;
                    }
    

     15. 给定年月的前几个月时间

            /**
             * @param $year 给定的年份
             * @param $month 给定的月份
             * @param $legth 筛选的区间长度 取前六个月就输入6
             * @param int $page 分页
             * @return array
             */
            function getLastTimeArea($year, $month, $legth, $page = 1)
            {
                    if (!$page)
                    {
                            $page = 1;
                    }
                    $monthNum = $month + $legth - $page * $legth;
                    $num = 1;
                    if ($monthNum < -12)
                    {
                            $num = ceil($monthNum / (-12));
                    }
                    $timeAreaList = [];
                    for ($i = 0; $i < $legth; $i++)
                    {
                            $temMonth = $monthNum - $i;
                            $temYear = $year;
                            if ($temMonth <= 0)
                            {
                                    $temYear = $year - $num;
                                    $temMonth = $temMonth + 12 * $num;
                                    if ($temMonth <= 0)
                                    {
                                            $temMonth += 12;
                                            $temYear -= 1;
                                    }
                            }
                            $temMonth = str_pad($temMonth, 2, '0', STR_PAD_LEFT);
                            //$startMonth = strtotime($temYear . '-' . $temMonth . '-01');//该月的月初时间戳
                            //$endMonth = strtotime($temYear . '-' . ($temMonth + 1) . '-01') - 86400;//该月的月末时间戳
    
                            $startMonth = strtotime($temYear . '-' . $temMonth . '-01');
                            $endMonth   = strtotime($temYear . '-' . $temMonth . '-' . date('t', $startMonth));
                            $res['startMonth'] = $temYear . '-' . $temMonth . '-01'; //该月的月初格式化时间
                            $res['endMonth'] = date('Y-m-d', $endMonth);//该月的月末格式化时间
                            $res['timeArea'] = implode(',', [$startMonth, $endMonth]);//区间时间戳
                            $timeAreaList[] = $res;
                    }
                    return array_reverse($timeAreaList);
                    //return $timeAreaList;
            }
    
  • 相关阅读:
    LPTHW 笨办法学python 20章
    LPTHW 笨方法学python 19章
    LPTHW 笨方法学python 18章
    LPTHW 笨方法学习python 16章
    hadoop删除节点。
    url中的百分号转译
    thrift编译安装
    python学习:函数的学习
    jsp静态导入和动态导入 笔记
    简要描述cookie和session的区别:
  • 原文地址:https://www.cnblogs.com/zhuiluoyu/p/4468958.html
Copyright © 2011-2022 走看看