我想创建一个函数,该函数根据年份输出月份的第一个日期和月份的最后日期的列表,该年份作为参数给出。
function first_and_last_dates_of_month($year){
//code
}
first_and_last_dates_of_month(2014);
我希望输出的内容如下:
2014-01-01
2014-01-31
2014-02-01
2014-02-28
2014-03-01
2014-03-31
2014-04-01
2014-04-30
2014-05-01
2014-05-31
2014-06-01
2014-06-30
2014-07-01
2014-07-31
2014-08-01
2014-08-31
2014-09-01
2014-09-30
2014-10-01
2014-10-31
2014-11-01
2014-11-30
2014-12-01
2014-12-31
答案 0 :(得分:1)
希望它会对你有所帮助:
function first_and_last_dates_of_month($year=2014){
for ($i=1; $i<=12; $i++) {
$timestamp = strtotime(date("$i/01/y"));
echo "<br/>";
echo $first_second = date('Y-m-01', $timestamp);
echo "<br/>";
echo $last_second = date('Y-m-t', $timestamp);
}
}
first_and_last_dates_of_month(2014);
答案 1 :(得分:0)
这也是一种可能性。
$year = date('Y');
$array = array(
1 => ''.$year.'-01-01',
2 => ''.$year.'-01-'.cal_days_in_month(CAL_GREGORIAN, 1, $year),
3 => ''.$year.'-02-01',
4 => ''.$year.'-02-'.cal_days_in_month(CAL_GREGORIAN, 2, $year),
5 => ''.$year.'-03-01',
6 => ''.$year.'-03-'.cal_days_in_month(CAL_GREGORIAN, 3, $year),
7 => ''.$year.'-04-01',
8 => ''.$year.'-04-'.cal_days_in_month(CAL_GREGORIAN, 4, $year),
9 => ''.$year.'-05-01',
10 => ''.$year.'-05-'.cal_days_in_month(CAL_GREGORIAN, 5, $year),
11 => ''.$year.'-06-01',
12 => ''.$year.'-06-'.cal_days_in_month(CAL_GREGORIAN, 6, $year),
13 => ''.$year.'-07-01',
14 => ''.$year.'-07-'.cal_days_in_month(CAL_GREGORIAN, 7, $year),
15 => ''.$year.'-08-01',
16 => ''.$year.'-08-'.cal_days_in_month(CAL_GREGORIAN, 8, $year),
17 => ''.$year.'-09-01',
18 => ''.$year.'-09-'.cal_days_in_month(CAL_GREGORIAN, 9, $year),
19 => ''.$year.'-10-01',
20 => ''.$year.'-10-'.cal_days_in_month(CAL_GREGORIAN, 10, $year),
21 => ''.$year.'-11-01',
22 => ''.$year.'-11-'.cal_days_in_month(CAL_GREGORIAN, 11, $year),
23 => ''.$year.'-12-01',
24 => ''.$year.'-12-'.cal_days_in_month(CAL_GREGORIAN, 12, $year));
var_dump($array);