轮子:php时间操作类

五、根据日期获取星期几。

  1. /**
  2.      * 获取星期几
  3.      * @param $date
  4.      * @return
  5.      */
  6.     public static function getWeekDay($date) {
  7.         //强制转换日期格式
  8.         $dateStr=date('Y-m-d',strtotime($date));
  9.         //封装成数组
  10.         $arr=explode("-"$dateStr);
  11.         //参数赋值
  12.         //年
  13.         $year=$arr[0];
  14.         //月,输出2位整型,不够2位右对齐
  15.         $month=sprintf('%02d',$arr[1]);
  16.         //日,输出2位整型,不够2位右对齐
  17.         $day=sprintf('%02d',$arr[2]);
  18.         //时分秒默认赋值为0;
  19.         $hour = $minute = $second = 0;
  20.         //转换成时间戳
  21.         $strap = mktime($hour,$minute,$second,$month,$day,$year);
  22.         //获取数字型星期几
  23.         $numberWk=date("w",$strap);
  24.         //自定义星期数组
  25.         $weekArr=array(7,1,2,3,4,5,6);
  26.         //获取数字对应的星期
  27.         return $weekArr[$numberWk];
  28.     }

六、获取指定日期前后相同时间天数的时间范围。

  1. /**
  2.      * 获取指定日期前后相同时间天数的范围时间
  3.      * @param int $dayDiff
  4.      * @param string $day
  5.      * @param string $dateFormat
  6.      * @return array
  7.      */
  8.     public static function getPointDaySameRangeContinuesTime($dayDiff = 0,$day = ""$dateFormat = "Y-m-d") {
  9.         $day = $day?$day:date($dateFormat);
  10.         $startTime = date($dateFormat,strtotime("$day -$dayDiff day"));
  11.         $endTime = date($dateFormat,strtotime("$day +$dayDiff day"));
  12.         $result = self::getContinuesDayByRange($startTime,$endTime,$dateFormat = 'Y-m-d');
  13.         return $result;
  14.     }

七、获取两个日期之间相差的天数。

  1. /**
  2.      * 获取两个日期之间相差的天数
  3.      * @param string $day1 第一个日期,格式为Y-m-d
  4.      * @param string $day2 第二个日期,格式为Y-m-d
  5.      * @return integer
  6.      */
  7.     public static function getDiffBetweenTwoDays($day1$day2) {
  8.         $second1 = strtotime($day1);
  9.         $second2 = strtotime($day2);
  10.         if ($second1 < $second2) {
  11.             $tmp = $second2;
  12.             $second2 = $second1;
  13.             $second1 = $tmp;
  14.         }
  15.         return ($second1 - $second2) / 86400;
  16.     }

八、根据指定日期和天数,获取结束的日期。

  1. /**
  2.      * 根据日期和相差的天数获取结束的天数
  3.      * @param $day
  4.      * @param $diffDay
  5.      * @param bool $isBefore
  6.      * @return false|string
  7.      */
  8.     public static function getEndDayByDayAndDiff($day$diffDay$isBefore = false) {
  9.         $operator = $isBefore ? "-" : "+";
  10.         $endDay = date('Y-m-d',strtotime("$day $operator $diffDay day"));
  11.         return $endDay;
  12.     }

九、判断两个日期是否为同一天。

  1. /**
  2.      * 判断两个时间是否同一天
  3.      * @param string $date1 Y-m-d
  4.      * @param string $date2 Y-m-d
  5.      * @return bool
  6.      */
  7.     public static function isSameDay($date1$date2) {
  8.         $day1 = self::dateTime(strtotime($date1)) ;
  9.         $day2 = self::dateTime(strtotime($date2));
  10.         return $day1 == $day2;
  11.     }

十、转换秒钟为分钟。

  1. /**
  2.      * 转换秒钟为分钟
  3.      * @param $seconds
  4.      * @return string
  5.      */
  6.     public static function convertSecondToTime($seconds) {
  7.         $reminded = strval($seconds % 60);
  8.         $minute = strval(($seconds - $reminded) / 60);
  9.         if(strlen($minute)<2){
  10.             $minute = '0'.$minute;
  11.         }
  12.         if(strlen($reminded)<2){
  13.             $reminded = '0'.$reminded;
  14.         }
  15.         $time = $minute.":".$reminded;
  16.         return $time;
  17.     }

十一、获取毫秒数。

  1. /**
  2.      * 获取时间的毫秒数
  3.      * @return float
  4.      */
  5.     public static function millisecond() {
  6.         list($msec$sec) = explode(' ', microtime());
  7.         return (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  8.     }

 

你想把广告放到这里吗?

发表评论

您必须 登录 才能发表留言!