获取当天是本月的第几周
获取当天是本月的第几周,顺便了解令人困惑的 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;
}
/*
* 写法不够简洁 v1.0
public function getWeekV1(): int
{
$totalDay = date('t'); //月份总天数
$today = date('Y-m-d'); //月份中的第几天
$todayW = date('W', strtotime($today)); //获取当前周数
$firstDay = date('Y-m-01'); //本月第一天
$eomW = date('W', strtotime("$firstDay + 1 month -1 day")); //这个月最后一天的周数
$weekSum = floor($totalDay / 7); //本月总周数
//本月总周数 - (本月尾周数 - 当前周数) + 1
$week = intval(($weekSum - ($eomW - $todayW)) + 1);
return $week;
}
*/
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: