使用php,填充当月的天数 [英] With php, populate an array of days in current month

查看:68
本文介绍了使用php,填充当月的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何使用PHP生成每月的天数?

在使用PHP的情况下,给定一个月的格式为:10月10日,11月11日至11月,我将如何填充一个数组,其中键代表给定月份的每一天。

With PHP, given a month in this format: 10 - October, 11 - November, how would I populate an array where the keys represent each day for the given month.

因此,

例如,对于2月,您将有一个键为1-28的数组。

e.g for February you'd have an array with keys 1-28.

推荐答案

您可以使用以下功能:

function dates_month($month, $year) {
    $num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $dates_month = array();

    for ($i = 1; $i <= $num; $i++) {
        $mktime = mktime(0, 0, 0, $month, $i, $year);
        $date = date("d-M-Y", $mktime);
        $dates_month[$i] = $date;
    }

    return $dates_month;
}

echo"<pre>"; 
print_r(dates_month(2, 2012));
echo"</pre>"; 

这篇关于使用php,填充当月的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆