php获取一年中的第x周是在几月
微wx笑 2022-04-04【编程语言】 2 0关键字: php
php获取一年中的第x周是在几月,求一年中的第$week周属于几月份
php获取一年中的第x周是在几月,求一年中的第$week周属于几月份
<?php //$debug_state = true; //注释掉这一句就不会打印Debug信息 function dbglog($msg){ global $debug_state; if (isset($debug_state)){ echo($msg . "<br><br>"); } } function getYear(){ return date("Y", time()); } /** * 求两个日期之间相差的天数 * (针对1970年1月1日之后,求之前可以采用泰勒公式) * @param string $day1 * @param string $day2 * @return number */ function diffBetweenTwoDays ($day1, $day2) { $second1 = strtotime($day1); $second2 = strtotime($day2); //if ($second1 < $second2) { // $tmp = $second2; // $second2 = $second1; // $second1 = $tmp; //} return abs($second1 - $second2) / 86400; } /** * 求$year年一共有多少天 */ function getTotalDay($year){ $day1 = $year . "-01-01"; $day2 = $year . "-12-31"; $totalDay = diffBetweenTwoDays($day1, $day2); return $totalDay + 1; } /** * 求一年中的$month月有多少天 */ function getDayCountOfMonth($year, $month){ $totalDay = getTotalDay($year); $mday = 31; //当前月分有多少天 $arrMD = [0, 31,28,31,30,31,30,31,31,30,31,30,31]; if ($month == 2){ if ($totalDay == 366){ $mday = 29; }else{ $mday = 28; } }else{ $mday = $arrMD[$month]; } //echo $month . "月有" . $mday . "天<br><br>"; //echo $month . "月的第" . $week . "周的星期". $wknum . "是" . $day . "日<br><br>"; return $mday; } /** * 求一年中的第$week周属于几月份 */ function getWeekOfMonth($week){ $tm = time(); //$tm = strtotime("2022-03-27 21:30:33"); $year = date("Y", $tm); $totalDay = getTotalDay($year);//$year年一共有多少天 dbglog($year . "年一共有" . $totalDay . "天"); $weekOfYear = intval(date('W',$tm)); //获取目前是一年中的第几周 dbglog("目前是一年中的第" . $weekOfYear . "周"); $wkDate = strtotime("-" . ($weekOfYear - $week) * 7 . " day",$tm); $wknum = date("w", $tm); //今天是星期几,1=周一,,,6=周六,0=周日 dbglog("第" . $week . "周的星期" . $wknum . "是" . date("Y-m-d", $wkDate)); $month = intval(date("m", $wkDate)); dbglog("第" . $week . "周的星期" . $wknum . "是在" . date("m", $wkDate) . "月"); $mday = getDayCountOfMonth($year, $month); //当前月分有多少天 dbglog($month . "月有" . $mday . "天"); $day = intval(date("d", $wkDate)); //今天是几号 dbglog("第" . $week . "周的星期". $wknum . "是" . $month . "月的" . $day . "日"); $syday = $mday - $day; //这个月还剩余天数 dbglog("这个月还剩余" . $syday . "天"); $m = $month; switch($wknum){ case 0: if ($day < 5){ $m = $month - 1; } break; case 6: if ($day < 4){ $m = $month - 1; } break; case 5: if ($day < 3){ $m = $month - 1; } break; case 4: if ($day < 2){ $m = $month - 1; } break; case 3: if ($day < 1){ $m = $month - 1; } break; case 2: if ($syday + $wknum < 3){ $m = $month+1; } break; case 1: if ($syday + $wknum < 2){ $m = $month+1; } break; default: break; } return $m; } ?>
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/lang/2022-04-04/1142.html