Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all days and date for a given month

Tags:

arrays

date

php

Would like to retrieve all days and date for a given month. Have this currently which shows all the days for the current month, but how do I parse in a specified month instead?

$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-D', $time);
}
echo "<pre>";
print_r($list);
echo "</pre>";
like image 719
user3543512 Avatar asked May 21 '14 10:05

user3543512


People also ask

How can I get number of days in a month in PHP?

The cal_days_in_month() function returns the number of days in a month for a specified year and calendar.

How to get the number of days in a month in JavaScript?

JavaScript getDate() Method: This method returns the number of days in a month (from 1 to 31) for the defined date. Return value: It returns a number, from 1 to 31, representing the day of the month.


2 Answers

try this

$list=array();
$month = 12;
$year = 2014;

for($d=1; $d<=31; $d++)
{
    $time=mktime(12, 0, 0, $month, $d, $year);          
    if (date('m', $time)==$month)       
        $list[]=date('Y-m-d-D', $time);
}
echo "<pre>";
print_r($list);
echo "</pre>";
like image 115
fortune Avatar answered Oct 11 '22 00:10

fortune


try this

$month = "05";
$year = "2014";

$start_date = "01-".$month."-".$year;
$start_time = strtotime($start_date);

$end_time = strtotime("+1 month", $start_time);

for($i=$start_time; $i<$end_time; $i+=86400)
{
   $list[] = date('Y-m-d-D', $i);
}

print_r($list);

See Demo

like image 32
Satish Sharma Avatar answered Oct 11 '22 00:10

Satish Sharma