2016-06-07 41 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);