这些答案一个月中日期的最后一天的名单不太做我想做的:获取在PHP
Getting last month's date in php How to find the last day of the month from date?
我有下面的代码,是为了打印一个选择框从给定日期的月份日期的所有最后一天:
$last_case_date = $this->query_end_date();
$increm_date = "2011-06-01";
echo '<select name = "end_interval">';
$increm_date = date('Y-m-d', strtotime("$increm_date"));
while($increm_date <= $last_case_date) {
// next date is $increm_date + 1 month
$increm_date = date('Y-m-d', strtotime("$increm_date + 1 months"));
// then we want the last day of this new date
$month = substr($increm_date, 5, 2);
$year = substr($increm_date, 0, 4);
$increm_date = $this->last_day_of_month($month, $year);
echo '<option value = "'.$increm_date.'" selected = "selected"'.'>'.$increm_date.'</option>';
}
echo '</select>';
其中last_day_of_month
是这样的:
function last_day_of_month($month = '', $year = '') {
if(empty($month)) {
$month = date('m');
}
if(empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 months', $result));
return date('Y-m-d', $result);
}
我从here借来的。
出现这种情况奇怪的是,我得到这些日期:
2011-07-31 2011-08-31 2011-10-31
但没有2011-09-30
!这可能与9月份只有30天的事情有关吗?
任何人都可以发现问题吗?我一直盯着它很长一段时间....谢谢你:)。