2016-09-22 57 views
0

我有兩個日期如何獲得兩個日期

$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

需要一些更多的描述。你星期一開始? –

+0

是的......我需要在兩個日期之間得到幾周,差別可能是一年或兩年。我需要得到這兩個日期之間的所有星期以及本週的開始和結束日期也 – AftabHafeez

+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

感謝它爲我工作:) – AftabHafeez

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);