您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2024/3/11 8:33:10发布32次查看
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;    }
以上代码仅供参考,疏漏之处还请指出以便改进!
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product