获取当天是本月的第几周,顺便了解令人困惑的 strtotimestrtotime (“$firstDay + 1 month -1 day”) 这样的写法会有问题,大家可以去鸟哥博客看看令人困惑的 s
顺晟科技
2021-07-12 13:32:14
339
strtotime (“$firstDay + 1 month -1 day”) 这样的写法会有问题,大家可以去鸟哥博客看看
令人困惑的 strtotime
<?php
/**
* 获取当天是本月的第几周
*
* @return int
* @author Henry
*/
public function getWeek(): int
{
$todayW = date('W', strtotime('now')); //获取当前周数
//$eomW = date('W', strtotime("$firstDay + 1 month -1 day")); 这样的写法有问题大家可以去鸟哥的博客看看
$eomW = date('W', strtotime('last day of')); //获取月尾周数,正确写法
$weekSum = floor(date('t') / 7); //月份总天数 / 7 = 本月总周数
//本月总周数 - (本月尾周数 - 当前周数) + 1
$week = intval(($weekSum - ($eomW - $todayW)) + 1);
return $week;
}