• 根据同时间段获取不同的年月日季度周时间


    /**
    * 根据指定日期返回经过的年月
    * @param string $sDay	开始日期
    * @param string $eDay	结束日期
    * @returnse multitype:string	返回一维数组
    */
    public static function getPassMonth(string $sDay , string $eDay ){
        $date1_stamp=strtotime($sDay);
        $date2_stamp=strtotime($eDay );
        $pass = $date2_stamp-$date1_stamp;
        $passArr = array();
        $oneDay = 86400;
        $maxTime = 0;
        while ($maxTime<=$pass){
          $passArr[] = date('Y-m',$date1_stamp);
          $day = date('t',$date1_stamp);
          $mDay = $oneDay*$day;
          $maxTime += $mDay;
          $date1_stamp+=$mDay;
        }
        return $passArr;
    }
    
    /**
    * 根据指定日期段经过的年月日
    * @param string $stime	开始时间
    * @param string $ttime	结束时间
    */
    public static function getPassDay(string $stime,string $ttime){
    
      $s = strtotime($stime);
      $t = strtotime($ttime);
      if($s>$t)return [];
      $day = ($t-$s)/86400+1;
      $dateArr = array();
      for ($i = 0; $i< $day;$i++){
      $times = 86400*$i;
      $newTime = $s+$times;
      if($newTime<=$t) $dateArr[] = date('Y-m-d',$newTime);
      }
      return $dateArr;
      }
    
    /**
    * 根据给定的时间,返回经过的小时
    * @param string $stime	开始时间(小时)	例:"06:11:10"
    * @param string $ttime	结束时间(小时)	例:"23:59:59"
    * @return multitype:string
    */
    public static function getPassTime(string $stime,string $ttime){
    
      $stimeNum = date('H',strtotime($stime));
      $ttimeNum = date('H',strtotime($ttime));
      $hourArr = array();
      for($i=$stimeNum;$i<=$ttimeNum;$i++){
        $hourArr[] = (strlen($i) == 1)?'0'.$i:$i;
      }
      return $hourArr;
    }
    
    /**
    * 根据给定日期,返回开始小时和结束小时
    * @param string $stime	开始时间(小时)	例:"06:11:10"
    * @param string $ttime	结束时间(小时)	例:"23:59:59"
    * @return multitype:string
    */
    public static function getHourData(string $stime, string $ttime){
      $sHourNum = date('H',strtotime($stime));
      $tHourNum = date('H',strtotime($ttime));
      return array(
    
        'shour'=>$sHourNum,
    
        'thour'=>$tHourNum
    
      );
    }
    
    /**
    * 获取指定日期开始和结束日期
    * @param string $day 日期
    * @return multitype:string
    */
    public static function getWeekDay(string $day){
      $time = strtotime($day);
      $stime = date("Y-m-d",mktime(0, 0 , 0,date("m",$time),date("d",$time)-date("w",$time)+1,date("Y",$time)));
      $ttime = date("Y-m-d",mktime(23,59,59,date("m",$time),date("d",$time)-date("w",$time)+7,date("Y",$time)));
      return array('stime'=>$stime,'ttime'=>$ttime);
    }
    
    /**
    * 获取指定周的日期
    * @param string $day 日期
    * @return multitype:string
    */
    public static function getWeekPassDay(string $day){
      $dateArr = self::getWeekDay($day);
      return self::getPassDay($dateArr['stime'],$dateArr['ttime']);
    }
    
    
    /**
    * 获取日期信息
    * @param string $timeStr	指定时间戳
    * @param array $dateArr	传入内容信息
    * @return number
    */
    public static function getDateInfo(string $timeStr='',$dateArr = array()){
    
      if(empty($timeStr))$timeStr = time();
    
      //年
      $dateArr['cyear'] = date('Y',$timeStr);
      //数字月
      $dateArr['cmonth'] = date('m',$timeStr);
      //星期几
      $dateArr['cweek'] = date('N',$timeStr);
      //天
      $dateArr['cdate'] = date('Y-m-d',$timeStr);
      //数字时
      $dateArr['chour'] = date('H',$timeStr);
      return $dateArr;
    }
    
    /**
    * 获取季度信息
    * @param int $dmonth	月份
    * @return number
    */
    public static function getQuarterFun(int $dmonth){
    
      if($dmonth < 4){
        $quarter = 1;
      }else if($dmonth > 3 && $dmonth < 7){
        $quarter = 2;
      }else if($dmonth > 6 && $dmonth < 10){
        $quarter = 3;
      }else{
        $quarter = 4;
      }
      return $quarter;
    }
    
    /**
    * 返回季度经过的月份起止
    * @param int $unixTime	时间戳
    * @return multitype:string
    */
    public static function getQuarterPassMonth(int $unixTime){
    $quarter = self::getQuarterFun(date('m',$unixTime));
    if(1== $quarter){
    $sMonth = '-01-01';
    $tMonth = '-03-31';
    }elseif(2 == $quarter){
    $sMonth = '-04-01';
    $tMonth = '-06-30';
    }elseif(3 == $quarter){
    $sMonth = '-07-01';
    $tMonth = '-09-30';
    }elseif(4 == $quarter){
    $sMonth = '-10-01';
    $tMonth = '-12-31';
    }
    return array('smonth'=>$sMonth,'tmonth'=>$tMonth);
    }
    
    
    /**
    * 返回某年某月第几个周几是哪一天
    *
    * @param int $moon	月份
    * @param int $num	第几个
    * @param string $week	周几
    * @param string $f	返回日期格式
    * @param string $year	返回哪一年的,默认是当前年份
    * @return multitype:boolean string
    */
    public static function week(int $moon=1,int $num=1,string $week='7',string $f='Y-m-d',string $year=NULL){
    
    if(empty($year))$year = date('Y');
    $times = $year.'-'.$moon;
    $t = date('t',strtotime($times));
    $ws = array();
    for($i=1;$i<=$t;$i++){
    $date=$times.'-'.$i;
    $w=date('N',strtotime($date));
    if ($w==$week)$ws[] = $date;
    }
    if(isset($ws[$num-1]))return array('success'=>true,data=>date($f,strtotime($ws[$num-1])));
    $msg = "当前信息错,{$year}年{$moon}月第{$num}个周{$week}不存在";
    return array('success'=>false,'data'=>$msg);
    }
    
    /**
    * 获取距离当前多少时间
    * @param string $time	时间字符串
    */
    public static function gettimelast(string $time){
    $now = time();
    $time = strtotime($time);
    $t = $now-$time;
    if($t<60){
    return '<span style="color:green">'.$t.'秒前</span>';
    }
    if($t<3600){
    return '<span style="color:#ff892a">'.ceil($t/60).'分钟前</span>';
    }
    if($t<86400){
    return '<span style="color:#478fca">'.ceil($t/60/60).'小时前</span>';
    }
    return date('Y-m-d H',$time);
    }
    
    
    /**
    * 获取某一天是每年的第几周,解决跨年问题
    * @param string $time 时间戳
    * @return string
    */
    public static function get_time_week(string $time){
    $year = date('Y',$time);
    $week = date('W',$time);
    if($week == '01'){
    $month = date('m',$time);
    if($month == '12'){
    $year = $year+1;
    }
    }
    return $year.$week;
    }
    
    
    /**
    * 获取一年多少周,每周的开始日期和结束日期
    * @param string $year
    * @return multitype:mixed
    */
    public static function get_week(string $year = '') {
    if(empty($year)) $year = date('Y');
    $weeks = date("W", mktime(0,0,0,12,28,$year));
    for($i = 1;$i<=$weeks;$i++){
    $m = $i;
    if($m<10){
    $m = '0'.$m;
    }
    $timestamp['start'] = strtotime($year.'W'.$m);
    $timestamp['end'] = strtotime('+6 day',$timestamp['start']);
    $weekArr[$i] = array(date("Y-m-d",$timestamp['start']), date("Y-m-d",$timestamp['end']));
    }
    return $weekArr;
    }
    
    
    /**
    * 获取一周的起始日期
    * @param unknown $week	例如:201552
    * @return string[]
    */
    public static function get_week_st(string $week){
    $year = substr($week,0,4);
    $week = substr($week,4,6);
    $timestamp['start'] = strtotime($year.'W'.$week);
    $timestamp['end'] = strtotime('+1 week -1 day',$timestamp['start']);
    return array(date("Y-m-d",$timestamp['start']),date("Y-m-d",$timestamp['end']));
    }
    
    
    /**
    * 获取两天之间的间隔天数
    * @param string $date1 开始时间
    * @param string $date2 结束时间
    * @return number
    */
    public static function dateDiff(string $date1, string $date2) {
    
    $datetime1 = new DateTime($date1);
    $datetime2 = new DateTime($date2);
    $interval = $datetime1->diff($datetime2);
    return $interval->format('%a');
    }
    
    
    /**
    * 获取一年内每月的起止日期
    * @param string $year
    * @return multitype:multitype:string
    */
    public static function get_month(string $year = ''){
    if(empty($year)) $year = date('Y');
    $tmp = array();
    for($i=1;$i<13;$i++){
    $firstday = $year.'-'.($i<10?'0'.$i:$i).'-01';
    $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));
    $tmp[$i] = array($firstday, $lastday);
    }
    return $tmp;
    }
    /**
    * 获取一年内每季度的起止日期
    * @param string $year
    * @return multitype:NULL
    */
    public static function get_quarter(string $year = ''){
    if(empty($year)) $year = date('Y');
    $month = self::get_month($year);
    $quarter_array = array();
    for($i=1;$i<5;$i++){
    $m = $i==1?$i:$m+3;
    $quarter_array[$i] = array(
    $month[$m][0],
    $month[($m)+2][1]
    );
    }
    return $quarter_array;
    }
    
    
    /**
    * 获取一年的起止日期
    * @param string $year
    * @return multitype:NULL
    */
    public static function get_year(string $year = ''){
    if(empty($year)) $year = date('Y');
    $month = self::get_month($year);
    return array($month[1][0],$month[12][1]);
    }
    
    
    /**
    * 根据指定日期段经过的年-周
    * @param string $stime	开始时间
    * @param string $ttime	结束时间
    */
    public static function getPastYearWeek(string $stime,string $ttime){
      $s = strtotime($stime);
      $t = strtotime($ttime);
      $day = (($t-$s)/(86400*7))+1;
      $dateArr = array();
        for ($i = 0; $i< $day;$i++){
    
          $times = 86400*$i*7;
          $newTime = $s+$times;
        if($newTime<=$t) $dateArr[] = date('Y-W',$newTime);
    }
    return $dateArr;
    }
    
    /**
    * 获取两个时间段经过的小时(Y-m-d H)格式
    */
    public static function getPastYmdh(string $stime,string $ttime){
      $s = strtotime($stime);
      $t = strtotime($ttime);
      if($s>$t)return [];
      $day = ($t-$s)/3600+1;
      $dateArr = array();
        for ($i = 0; $i< $day;$i++){
          $times = 3600*$i;
          $newTime = $s+$times;
        if($newTime<=$t) $dateArr[] = date('Y-m-d H',$newTime);
    }
    return $dateArr;
    }
    
     
    

      

  • 相关阅读:
    第二周学习总结
    调查问卷
    第五周学习总结
    基于DevExpress的项目窗体统一换肤
    一个Linq to Sql 的关联小问题,搞死人
    VC中类型转换(转载)
    保证一个程序只运行一次
    给所有的Control添加发送键盘事件Tab事件,实现回车键自动跳转到下一个控件[基于Shark Xu]
    遍历进程
    CSS Friendly ASP.NET 2.0 Control Adapters (Beta 2.0)在处理URL时的一个Bug
  • 原文地址:https://www.cnblogs.com/love1226/p/8757670.html
Copyright © 2020-2023  润新知