我有两个约会
$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));
任何人都建议我用周数得到确切的年份
答案 0 :(得分:3)
首先,您需要将时间转换为对象,之后计算差异。现在计算给定日期之间的周数。现在是从开始日期开始的几个星期的时间循环,我使用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
答案 1 :(得分:0)
$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);