$startDate = "2014-03-01";
$endDate= "2014-05-25";
所需结果:3月,4月,5月;
答案 0 :(得分:5)
为PHP提供DatePeriod对象。请看下面的例子。
$period = new DatePeriod(
new DateTime('2014-03-01'),
DateInterval::createFromDateString('1 month'),
new DateTime('2014-05-25')
);
foreach ($period as $month) {
echo strftime('%B', $month->format('U'));
}
答案 1 :(得分:1)
快速解决方案是每天解析并检查月份:
$startDate = "2014-03-01";
$endDate = "2014-05-25";
$start = strtotime($startDate);
$end = strtotime($endDate);
$result = array();
while ( $start <= $end )
{
$month = date("M", $start);
if( !in_array($month, $result) )
$result[] = $month;
$start += 86400;
}
print_r($result);
我相信通过新的OOP(DateTime对象)方法可以提高效率,但是如果你需要让它工作的话,这是快速且无脑的。
答案 2 :(得分:0)
$date = date('F',strtotime($startDate));
完整月份代表(即Januaray,二月等)
$date = date('M',strtotime($startDate));
缩写为...(即1月,2月,3月)
如果你想根据两个日期回应那些月份......
$d = "2014-03-01";
$startDate = new DateTime($d);
$endDate = new DateTime("2014-05-01");
function diffInMonths(DateTime $date1, DateTime $date2)
{
$diff = $date1->diff($date2);
$months = $diff->y * 12 + $diff->m + $diff->d / 30;
return (int) round($months);
}
$t = diffInMonths($startDate, $endDate);
for($i=0;$i<$t+1;$i++){
echo date('F',strtotime($d. '+'.$i.' Months'));
}
答案 3 :(得分:0)
<?php
$startDate = "2014-03-01";
echo date('F',strtotime($startDate));
?