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
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
如何修復KB5055612無法在Windows 10中安裝?
4 週前ByDDD
<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
4 週前By尊渡假赌尊渡假赌尊渡假赌
<🎜>:種植花園 - 完整的突變指南
3 週前ByDDD
北端:融合系統,解釋
4 週前By尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!