Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php array containing all dates of the current month [closed]

Tags:

arrays

date

php

I'm trying and still wondering, how I can get an array which contains all of the dates in the current month, it should contain all of the dates in format: year-month-day. Thanks for your help!

like image 778
Moozy Avatar asked Nov 12 '12 15:11

Moozy


3 Answers

a simple function to return such an array could look like this:

function range_date($first, $last) {
  $arr = array();
  $now = strtotime($first);
  $last = strtotime($last);

  while($now <= $last ) {
    $arr[] = date('Y-m-d', $now);
    $now = strtotime('+1 day', $now);
  }

  return $arr;
}

if needed, you can improve it by changing the step (+1 day) and the output format (Y-m-d) into optional parameters.

like image 143
oezi Avatar answered Nov 08 '22 02:11

oezi


Try:

// for each day in the month
for($i = 1; $i <=  date('t'); $i++)
{
   // add the date to the dates array
   $dates[] = date('Y') . "-" . date('m') . "-" . str_pad($i, 2, '0', STR_PAD_LEFT);
}

// show the dates array
var_dump($dates);
like image 23
Teena Thomas Avatar answered Nov 08 '22 02:11

Teena Thomas


How about this:

$list=array();
for($d=1; $d<=31; $d++)
{
    $time=mktime(12, 0, 0, date('m'), $d, date('Y'));
    if (date('m', $time)==date('m'))
        $list[]=date('Y-m-d', $time);
}
var_dump($list);
like image 42
Wolfgang Stengel Avatar answered Nov 08 '22 01:11

Wolfgang Stengel