php 如何在两个日期之间获取一周的开始和结束

92vpleto  于 2023-06-04  发布在  PHP
关注(0)|答案(3)|浏览(119)

我有两个约会

$start_date = '2015-09-21';
$end_Date = '2016-09-21';

我想得到的是这样

Week  =  Monday           Friday 
36    =  2015-09-21       2015-09-25
37    =  2015-09-28       2015-10-02
38    =  2015-10-05       2015-10-09
.
.
38   =  2016-09-19        2016-09-24

日期差异可以是一年或更长。我需要得到的是确切的每个星期一和星期五的日期之间的这个时间间隔。
我用了这个方法

$weeks = array();
    while ($start_date < $end_Date )
    {  
        $weeks[] = date('W', $start_date ); 
        $start_date += strtotime('+1 week', 0);
    }

    function getStartAndEndDate($week, $year)
    {

        $time = strtotime("1 January $year", time());
        $day = date('w', $time);
        $time += ((7*$week)+1-$day)*24*3600;
        $return[0] = date('Y-m-d', $time);
        $time += 6*24*3600;
        $return[1] = date('Y-m-d', $time);
        return $return;
    }

当我调用这个函数时,我需要输入周数和年份来获得确切的日期。但是我不能用特定的一周来获得年份。我可以设法从开始日期或结束日期得到一周。

$current_year = date("Y", strtotime($fromdate));

有没有人建议我用星期数来得到确切的年份

e4yzc0pl

e4yzc0pl1#

首先,你需要将时间转换为对象,然后计算差值。现在计算给定日期之间的周数。现在它的时间循环从开始日期开始,我使用DateInterval('P4D')将日期提前4天,然后回显两个日期,然后再次使用DateInterval('P3D')完成一周,重复此操作。

$start_date = '2015-09-21';
$end_Date = '2016-09-21';

$date1 = new DateTime($start_date);
$date2 = new DateTime($end_Date);
$interval = $date1->diff($date2);

$weeks = floor(($interval->days) / 7);

for($i = 1; $i <= $weeks; $i++){    
    $week = $date1->format("W");
    $date1->add(new DateInterval('P4D'));
    echo $week." = ".$start_date." - ".$date1->format('Y-m-d')."<br/>";
    $date1->add(new DateInterval('P3D'));
    $start_date = $date1->format('Y-m-d');
}

输出:

39 = 2015-09-21 - 2015-09-25
40 = 2015-09-28 - 2015-10-02
41 = 2015-10-05 - 2015-10-09
42 = 2015-10-12 - 2015-10-16
43 = 2015-10-19 - 2015-10-23
44 = 2015-10-26 - 2015-10-30
45 = 2015-11-02 - 2015-11-06
46 = 2015-11-09 - 2015-11-13
47 = 2015-11-16 - 2015-11-20
48 = 2015-11-23 - 2015-11-27
49 = 2015-11-30 - 2015-12-04
50 = 2015-12-07 - 2015-12-11
51 = 2015-12-14 - 2015-12-18
52 = 2015-12-21 - 2015-12-25
53 = 2015-12-28 - 2016-01-01
01 = 2016-01-04 - 2016-01-08
02 = 2016-01-11 - 2016-01-15
03 = 2016-01-18 - 2016-01-22
04 = 2016-01-25 - 2016-01-29
05 = 2016-02-01 - 2016-02-05
06 = 2016-02-08 - 2016-02-12
07 = 2016-02-15 - 2016-02-19
08 = 2016-02-22 - 2016-02-26
09 = 2016-02-29 - 2016-03-04
10 = 2016-03-07 - 2016-03-11
11 = 2016-03-14 - 2016-03-18
12 = 2016-03-21 - 2016-03-25
13 = 2016-03-28 - 2016-04-01
14 = 2016-04-04 - 2016-04-08
15 = 2016-04-11 - 2016-04-15
16 = 2016-04-18 - 2016-04-22
17 = 2016-04-25 - 2016-04-29
18 = 2016-05-02 - 2016-05-06
19 = 2016-05-09 - 2016-05-13
20 = 2016-05-16 - 2016-05-20
21 = 2016-05-23 - 2016-05-27
22 = 2016-05-30 - 2016-06-03
23 = 2016-06-06 - 2016-06-10
24 = 2016-06-13 - 2016-06-17
25 = 2016-06-20 - 2016-06-24
26 = 2016-06-27 - 2016-07-01
27 = 2016-07-04 - 2016-07-08
28 = 2016-07-11 - 2016-07-15
29 = 2016-07-18 - 2016-07-22
30 = 2016-07-25 - 2016-07-29
31 = 2016-08-01 - 2016-08-05
32 = 2016-08-08 - 2016-08-12
33 = 2016-08-15 - 2016-08-19
34 = 2016-08-22 - 2016-08-26
35 = 2016-08-29 - 2016-09-02
36 = 2016-09-05 - 2016-09-09
37 = 2016-09-12 - 2016-09-16
nxowjjhe

nxowjjhe2#

如果开始日期是1月1日,而结束日期是前一年的第52周,那么“Yogesh Singasane”提供的答案就不起作用了。下面是修改后的代码,似乎涵盖了相同的角落情况:

$start_date = '2023-01-01';
$end_Date = '2023-01-31';

$startTime = strtotime($start_date);
$endTime = strtotime($end_Date);

$weeks = array();
$date = new DateTime();
$i=0;
while ($startTime < $endTime) {  
    $weeks[$i]['week'] = date('W', $startTime);
    if(date("m-d", strtotime($start_date)) == '01-01' && date('W', $startTime) >= 52){
        $weeks[$i]['year'] = date('Y', $startTime) - 1 ; 
    }
    else{
        $weeks[$i]['year'] = date('Y', $startTime); 
    }
    $date->setISODate($weeks[$i]['year'], $weeks[$i]['week']);
    $weeks[$i]['Monday']=$date->format('Y-m-d'); 
    $weeks[$i]['Friday'] = date('Y-m-d',strtotime($weeks[$i]['Monday'] . "+4 days"));
    $startTime += strtotime('+1 week', 0);
    $i++;
}
var_dump($weeks);

P.S. Google把我带到这里,我想发帖帮助像我一样的人。

laik7k3q

laik7k3q3#

$start_date = '2015-09-21';
$end_Date = '2016-09-21';

$startTime = strtotime($start_date);
$endTime = strtotime($end_Date);

$weeks = array();
$date = new DateTime();
$i=0;
while ($startTime < $endTime) {  
    $weeks[$i]['week'] = date('W', $startTime);
    $weeks[$i]['year'] = date('Y', $startTime); 
    $date->setISODate($weeks[$i]['year'], $weeks[$i]['week']);
    $weeks[$i]['Monday']=$date->format('Y-m-d'); 
    $weeks[$i]['Friday'] = date('Y-m-d',strtotime($weeks[$i]['Monday'] . "+4 days"));
    $startTime += strtotime('+1 week', 0);
    $i++;
}
var_dump($weeks);

相关问题