2016-06-07 38 views
0

我有兩個日期PHP打印日期的差異,根據太月

2016-06-22 , 2016-07-11

我需要打印在例如數日,

22,23,24,25,26,27,28,29,30,1,2,.....11

如果月份是七月八月它應該打印

22,23,24,25,26,27,28,29,30,31,1,2,.....11

根據明月還在PHP中。

謝謝。

+2

你到目前爲止試過的是什麼?發佈您的嘗試。 –

+0

先生,我不知道如何做到這一點,我只做了日期差異。 – Crysis

+1

[PHP:可能重複數組中兩個日期之間的所有日期](http://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array) –

回答

3

這會爲你工作.. Look The DatePeriod class

日期允許迭代一組日期和時間,在給定的時間段內定期循環。

<?php 

$begin = new DateTime('2016-06-22'); 
$end = new DateTime('2016-07-11'); 
$end = $end->modify('+1 day'); 

$interval = new DateInterval('P1D'); 
$daterange = new DatePeriod($begin, $interval ,$end); 

foreach($daterange as $date){ 
    echo $date->format("d") . "<br>"; 
} 
?> 

活生生的例子:CLICK HERE

+0

謝謝你這個作品 – Crysis

+0

@Crysis活的例子:[點擊這裏](https://eval.in/584588) –

+0

尊敬的@Crysis您可以接受最有幫助的答案... –

0
在MySQL

select date_format(my_date_column, '%d$) from my_table 

在PHP

$date = new DateTime('2016-06-22'); 
echo $date->format('d'); 

呼應天數

1

嘗試:

function createDateRangeArray($strDateFrom,$strDateTo) 
{ 

    // inclusive array of the dates between the from and to dates. 

    // could test validity of dates here but I'm already doing 
    // that in the main script 

    $aryRange=array(); 

    $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),  substr($strDateFrom,8,2),substr($strDateFrom,0,4)); 
    $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),  substr($strDateTo,8,2),substr($strDateTo,0,4)); 

    if ($iDateTo>=$iDateFrom) 
    { 
     array_push($aryRange,date('d',$iDateFrom)); // first entry 
     while ($iDateFrom<$iDateTo) 
     { 
      $iDateFrom+=86400; // add 24 hours 
      array_push($aryRange,date('d',$iDateFrom)); 
     } 
    } 
    return $aryRange; 
} 

$arr = createDateRangeArray("2016-06-22","2016-07-11"); 
echo implode(",",$arr); 
+0

檢查此:http://stackoverflow.com/a/4312491/2815635 – C2486

+0

謝謝你這個作品太 – Crysis

3

你必須遍歷開始和結束日期之間的日期,格式d打印。

$fromDate = new DateTime('2016-06-22'); 
$toDate = new DateTime('2016-07-11'); 

$days = array(); 
while($fromDate <= $toDate) { 
    $days[] = $fromDate->format('d'); 
    $fromDate->modify('tomorrow'); 
} 

echo implode(',', $days);