我有代码:
$dateporngprakfin = "2015-02-01"
for($i=1;$i<=6;$i++){
$dateporngprakfin = strtotime("+1 months", $dateporngprakfin);
$datesum = date('Y-m-d', $dateporngprakfin);
echo $datesum."<br>";
}
此代码将显示“2015-03-01”至“2015-08-01”,但我想按月计算日期。
答案 0 :(得分:1)
使用cal_days_in_month()函数。
$dateporngprakfin = date_create("2015-02-01");
for($i=1;$i<=6;$i++){
date_add($dateporngprakfin, date_interval_create_from_date_string('1 Months'));
$countDays = cal_days_in_month(CAL_GREGORIAN, date_format($dateporngprakfin, 'm'), date_format($dateporngprakfin, 'Y'));
echo "<br />Count Days in ".date_format($dateporngprakfin, 'Y-m')." is ".$countDays;
}
答案 1 :(得分:0)
嗨试试这个以获得所需的日期
$dateporngprakfin = "2013-12-31";
$monthsToAdvance=1;
function getDates($startDate, $monthsToAdvance)
{
$dt = new DateTime($startDate);
$day = $dt->format('d');
$dt->setDate($dt->format('Y'),$dt->format('n'),1);
$dt->add(new DateInterval('P'.$monthsToAdvance.'M'));
$daysInMonth = cal_days_in_month(CAL_GREGORIAN,$dt->format('n'), $dt->format('Y'));
if($day > $daysInMonth) $day = $daysInMonth;
$dt->setDate($dt->format('Y'),$dt->format('n'),$day);
return $dt->format('Y-m-d');
}
echo getDates($startDate, $monthsToAdvance)
有关详细信息here
,请参阅此页面答案 2 :(得分:0)
您可以尝试此操作来计算一个月内的天数
$number = cal_days_in_month(CAL_GREGORIAN, 8, 2003);
它将返回2003年8月份的天数