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

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

PHP 日期格式化示例代码:

   /**
     * 格式化时间
     * $type:类型
     * $strDate:需要处理的时间字符串
     *
     * 年份  Y:四位年份     y:两位年份
     * 月份  m: 两位数字月份 n: 一位数字月份 M:英文月
     * 日期  d:两位数字日期 j:一位数字日期  D:英文日期
     * 时:H(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 = '';

       /* 0:周末 1-6:周一 至 周六 */
       $weekIndex = (int)date('w');
       switch($weekIndex){
            case 0:
                $dateArr['W1'] = date('Y-m-d 00:00:00',strtotime('-6 day'));
                $dateArr['W7'] = date('Y-m-d 23:59:59');
                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;
    }

获取指定日期对应的星期:

 #region 获取指定日期星期
    /**
     * 获取指定日期星期
     *
     * @param $strDate string 时间
     *
     * @return string
     */
    public static function GetWeek($strDate = ''){
         $tim = time();
         if(!empty($strDate) && isset($strDate)){
             $tim = strtotime($strDate);
         }

         /* 0:周末 1-6:周一 至 周六 */
         $weekIndex = (int)date('w',$tim);
         switch($weekIndex){
             case 0:  return "周末";
             case 1:  return "周一";
             case 2:  return "周二";
             case 3:  return "周三";
             case 4:  return "周四";
             case 5:  return "周五";
             case 6:  return "周六";
             default: return "";
         }
    }
    #endregion

获取两时间相差的天数小时以及分秒:

#region 获取两时间相差的天数小时以及秒
    /**
     * 获取两时间相差的天数小时以及秒
     *
     * @param $begin_time string :开始时间
     * @param $end_time   string :结束时间
     *
     * @return array
     */
    public static function GetTimediff($begin_time,$end_time)
    {
        $starttime = null;$endtime=null;
        $begin_time = strtotime($begin_time);
        $end_time = strtotime($end_time);

        if($begin_time < $end_time){
            $starttime = $begin_time;
            $endtime = $end_time;
        }
        else{
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        $timediff = $endtime-$starttime;
        $days = intval($timediff/86400);
        $remain = $timediff%86400;
        $hours = intval($remain/3600);
        $remain = $remain%3600;
        $mins = intval($remain/60);
        $secs = $remain%60;

        return [
            "day"  => $days,
            "hour" => $hours,
            "min"  => $mins,
            "sec"  => $secs
        ];
    }
    #endregion


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



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追夢秋陽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值