我有两个日期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中。
谢谢。
我有两个日期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中。
谢谢。
这会为你工作.. 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
select date_format(my_date_column, '%d$) from my_table
在PHP
$date = new DateTime('2016-06-22');
echo $date->format('d');
呼应天数
尝试:
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);
你必须遍历开始和结束日期之间的日期,格式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);
你到目前为止试过的是什么?发布您的尝试。 –
先生,我不知道如何做到这一点,我只做了日期差异。 – Crysis
[PHP:可能重复数组中两个日期之间的所有日期](http://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array) –