Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With php, populate an array of days in current month [duplicate]

Tags:

php

calendar

Possible Duplicate:
how to generate monthly days with PHP?

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.

So,

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

like image 818
Andy Tait Avatar asked Oct 29 '12 09:10

Andy Tait


2 Answers

You can use this function:

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>"; 
like image 55
Vikas Umrao Avatar answered Oct 20 '22 02:10

Vikas Umrao


$date = '02 February';
$days = array_fill_keys(
    range(1, date('t', gmmktime(0, 0, 0, (integer) $date, 1))),
    NULL
);

Assumption, for this year (else February could be 28 or 29)

like image 22
Mark Baker Avatar answered Oct 20 '22 02:10

Mark Baker