本篇内容主要讲解“php如何获取日期是当月第几周”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“php如何获取日期是当月第几周”吧!
php获取日期是当月第几周的方法:首先创建一个PHP示例文件;然后通过“date('d', $time)-(8 - $wk_day);echo $day<=0 ? 1:ceil($day / 7)+1;”获取当天是本月第几周。
本文操作环境:windows7系统、PHP7.1版,DELL G3电脑
php怎么获取日期是当月第几周?
PHP获取日期在月份里属于第几周,最简单的获取当天是本月第几周
<?php$time = strtotime('2019-01-01');$wk_day = date('w', strtotime(date('Y-m-1 00:00:00', $time))) ? : 7; //今天周几$day = date('d', $time) - (8 - $wk_day); //今天几号echo $day <= 0 ? 1 : ceil($day / 7) + 1; //计算是第几个星期 print_r(week_of_month(strtotime('2019-01-1'))); function week_of_month($time, $offset = 1){ $week = get_week_block($time); $wk_day = date('w', strtotime(date('Y-m-1 00:00:00', $time))) ? : 7; //今天周几 $day = date('d', $time) - (8 - $wk_day); //今天几号 $weekof = $day <= 0 ? 1 : ceil($day / 7) + 1; //计算是第几个星期 if($offset){ $week[0] = date('Y-m-d', max(strtotime(date('Y-m-1 00:00:00', $time)), $week[0])); $week[1] = date('Y-m-d', min(strtotime(date('Y-m-'.date('t', $time).' 23:59:59', $time)), $week[1])); }else{ $week[0] = date('Y-m-d', $week[0]); $week[1] = date('Y-m-d', $week[1]); } return ['week' => $week, 'weekof' => $weekof];} function get_week_block($sdefaultDate = false, $first = 1){//当前日期 //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期 if(!$sdefaultDate)$sdefaultDate = time();if(is_numeric($sdefaultDate))$sdefaultDate = date('Y-m-d', $sdefaultDate);//获取当前周的第几天 周日是 0 周一到周六是 1 - 6 $w=date('w', strtotime($sdefaultDate)); //获取本周开始日期,如果$w是0,则表示周日,减去 6 天 $week_start=date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days')); //本周结束日期 $week_end=date('Y-m-d',strtotime("$week_start +6 days"));return array(strtotime($week_start.' 00:00:00'), strtotime($week_end.' 23:59:59'));}
到此,相信大家对“php如何获取日期是当月第几周”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!