PHP的打印日期差异按月计算

时间:2016-06-07 11:26:34

标签: php datetime

我有两个约会

2016-06-22 , 2016-07-11

我需要以数字打印日期,例如

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

如果月份是7月到8月那么应该打印

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

根据月份也在PHP中。

谢谢。

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);

答案 2 :(得分: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);

答案 3 :(得分:0)

在mysql中

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

在php中

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

回显日期编号