This question already has answers here:
Find weekly periods (starting on a Monday) for a month
                                
                                    (3个答案)
                                
                        
                                2年前关闭。
            
                    
我需要显示所选月份的相应星期中的日期范围。
假设选择的值是$month=2$year=2017
输出应显示所选特定月份中几周的日期范围列表。

$month = $_GET['month'];
$year = $_GET['year'];
 ...

Output:
Week 1: 01/02/2017 - 05/02/2017
Week 2: 06/02/2017 - 12/02/2017
Week 3: 13/02/2017 - 19/02/2017
Week 4: 20/02/2017 - 26/02/2017
Week 5: 27/02/2017 - 28/02/2017


我浏览过Split the current month in to weeks in phpGet week number in month from date in PHP?Split date ranges into corresponding weeks,但是它们都不适合我。

谢谢。

最佳答案

这是一种与strtotime一起循环的方法。

我在Unix上添加86400 * 7,要增加一个星期。

$month = 2;
$year = 2017;

$week = date("W", strtotime($year . "-" . $month ."-01")); // weeknumber of first day of month

Echo date("d/m/Y", strtotime($year . "-" . $month ."-01")) ." - "; // first day of month
$unix = strtotime($year."W".$week ."+1 week");
While(date("m", $unix) == $month){ // keep looping/output of while it's correct month

   Echo date("d/m/Y", $unix-86400) . "\n"; // Sunday of previous week
   Echo date("d/m/Y", $unix) ." - "; // this week's monday
   $unix = $unix + (86400*7);
}
Echo date("d/m/Y", strtotime("last day of ".$year . "-" . $month)); //echo last day of month


https://3v4l.org/LAMBl

关于php - 如何查找给定月份的星期日期范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47707545/

10-11 03:10
查看更多