我有兩個日期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) –