찾다
php教程php手册PHP 日期格式化和日期计算以及获取当前周、月头尾日期

PHP 日期格式化和日期计算以及当获取前周、月头尾日期

PHP 日期格式化示例代码:

   /**
     * 格式化时间
     * $type:类型
     * $strDate:需要处理的时间字符串
     *
     * 年份  Y:四位年份     y:两位年份
     * 月份  m: 两位数字月份 n: 一位数字月份 M:英文月
     * 日期  d:两位数字日期 j:一位数字日期  D:英文日期
     * 时:H 、分:i 、秒:s
     **/
    public function GetFormatDate($type = 1,$strDate=''){
            $time = time();
            if(isset($strDate) && !empty($strDate)){
                $time = strtotime($strDate);
            }

            switch($type){
                case 1:
                   return date("H:i",$time);
                case 2:
                    return date("m月d日 H:i",$time);
                case 3:
                    return date("m/d H:i",$time);
                case 4:
                    return date("Y年m月d日 H:i",$time);
                case 5:
                    return date("Y/m/d H:i",$time);
                case 6:
                    return date("Y年m月d日 H:i:s",$time);
                case 7:
                    return date("Y-m-d H:i:s",$time);
                case 8:
                    return date("Y/m/d H:i:s",$time);
                default:
                    return $strDate;
            }
    }

日期计算示例代码:

   /**
     * 时间加减处理
     * $strDate:需要处理的时间字符串
     * $days:   加减天数
     **/
    public function ChangeDate($strDate,$days){
          $time = time();
          if(isset($strDate) && !empty($strDate)){
              $time = strtotime($strDate);
          }

         return date('Y-m-d H:i:s',strtotime("$days day",$time));
    }

获取当前周、月头尾日期示例代码:

   /**
     *  获取当前周、月的头尾日期
     *
     *  $dateArr['W1']:周一
     *  $dateArr['W7']:周末
     *  $dateArr['M1']:月头
     *  $dateArr['M2']:月尾
     **/
    public function GetCurrentDateInfo(){
       $dayTimes = 24*60*60;
       $dateArr = [];$temp = '';

       $weekIndex = (int)date('w');
       switch($weekIndex){
            case 0:
                $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('+1 day'));
                $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+7 day'));
                break;
            case 1:
                $dateArr['W1'] = date('Y-m-d 00:00:00');
                $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+6 day'));
                break;
            case 2:
                $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-1 day'));
                $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+5 day'));
                break;
            case 3:
                $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-2 day'));
                $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+4 day'));
                break;
            case 4:
                $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-3 day'));
                $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+3 day'));
                break;
            case 5:
                $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-4 day'));
                $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+2 day'));
                break;
            case 6:
                $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-5 day'));
                $dateArr['W7'] = date('Y-m-d 23:59:59',strtotime('+1 day'));
                break;
        }


       //1-12:一月 至 十二月
       $monthIndex = (int)date('m');
       switch($monthIndex){
           case 1:
               $temp = date('Y-02-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 2:
               $temp = date('Y-03-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 3:
               $temp = date('Y-04-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 4:
               $temp = date('Y-05-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 5:
               $temp = date('Y-06-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 6:
               $temp = date('Y-07-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 7:
               $temp = date('Y-08-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 8:
               $temp = date('Y-09-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 9:
               $temp = date('Y-10-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 10:
               $temp = date('Y-11-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 11:
               $temp = date('Y-12-01 00:00:00');
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
           case 12:
               $temp = date((date('Y')+1)."-01-01 00:00:00");
               $dateArr['M1'] = date('Y-m-01 00:00:00');
               $dateArr['M2'] = date('Y-m-d 23:59:59',strtotime($temp)-$dayTimes);
               break;
       }


       return $dateArr;
    }

以上代码仅供参考,疏漏之处还请指出以便改进!




성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

Nordhold : Fusion System, 설명
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌
<exp exp> 모호한 : 원정 33- 완벽한 크로마 촉매를 얻는 방법
2 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.