獲取當天是本月的第幾周,順便了解令人困惑的 strtotime
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;
}