Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I just want to make an array with the dates of a month. So the dates will start from 1 until 31 at most.

The obvious way, I know is a manual way:

$date_array = array( '1', '2', '3', '4', '5', '6', '7', ..., '31' );

But I'm looking for an automated way, like a simple for loop, making me my desired automatically generated dates:

for($Idx=1;$Idx<32;$Idx++) {
  var_dump($Idx);
}

Just need to put them in an array.

IMAGINATION

$my_date_array = makeArray( $Idx );

makeArray() - hah! :)

Possible?

share|improve this question
2  
Thank you. That's the answer. :) – Mayeenul Islam Jul 17 at 10:25
@MayeenulIslam check my answer. – EpokK Jul 17 at 10:27

closed as off-topic by PeeHaa, Akam, andrewsi, Eric Brown, ryan1234 Jul 17 at 20:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – PeeHaa, Community, andrewsi, Eric Brown, ryan1234
If this question can be reworded to fit the rules in the help center, please edit the question.

4 Answers

up vote 6 down vote accepted

Use this:

function makeArray ($calendar, $month, $year) { 
   return range (1, cal_days_in_month ($calendar, $month, $year));
}

Example

$aMyArray = makeArray (CAL_GREGORIAN, 8, 2003);

References

  • range array range ( mixed $start , mixed $end [, number $step = 1 ] )
  • cal_days_in_month int cal_days_in_month ( int $calendar , int $month , int $year )
share|improve this answer
+1 looks like tested and well prepared example :) – d.raev Jul 17 at 10:28
+1, works exactly as expected. – Amal Murali Jul 17 at 10:29
Edited the answer: without returning the function was generating NULL. – Mayeenul Islam Jul 18 at 4:05
OK, it's done :) – EpokK Jul 18 at 6:20
function makeArray() {
    $months = array(31,28,31,30,31,30,31,31,30,31,30,31);
    $array = array();
    foreach($months as $month) {
        $array = array_merge($array, range(1, $month));
    }
    return $array;
}

This function takes into account that each month has a different number of days. Watch out for leap-years though!

If you need this from this year, use this. It takes care of leap-years for you!

function makeArray() {
    $array = array();
    for($month = 0; $month < 12; $month++) {
        $array = array_merge($array, range(1, date("t", mktime(0, 0, 0, $month, 1)));
    }
    return $array;
}
share|improve this answer
$date = time(); // A Unix timestamp, so for example 'strtotime("2013-01-01")'.
$array_of_days_in_month = range(1, date("t", $date));

date("t") will return the number of days in a given month.

share|improve this answer

use range():

$data = range(1,31);
share|improve this answer
take in mind this the first value will be with key 0 array(0=>1, 1=>2, ...30=>31); – d.raev Jul 17 at 10:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.