简体   繁体   中英

Get all days and date for a given month

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>";

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>";

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

$days = cal_days_in_month( 0, $month, $year);

cal_days_in_month: Return the number of days in a month for a given year and calendar.
First parameter is "calendar":

0 or CAL_GREGORIAN - Gregorian Calendar
1 or CAL_JULIAN - Julian Calendar
2 or CAL_JEWISH - Jewish Calendar
3 or CAL_FRENCH - French Revolutionary Calendar

http://php.net/manual/en/function.cal-days-in-month.php

http://php.net/manual/en/function.cal-info.php

for object oriented users,

function getDaysInYearMonth (int $year, int $month, string $format){
  $date = DateTime::createFromFormat("Y-n", "$year-$month");

    $datesArray = array();
    for($i=1; $i<=$date->format("t"); $i++){
        $datesArray[] = DateTime::createFromFormat("Y-n-d", "$year-$month-$i")->format($format);
    }

 return $datesArray;
}

I'm surprised nobody mentioned PHP's built-in, no extra extensions DateTime class.

<?php
$daysInAugust2020 = (new DateTime("20200801"))->format('t');
print_r($daysInAugust2020);

You'll have to append some day to your YYYYMM string, but it's not that hard: every month has a 1st. So you can always add 01 .

The trick here is the t format, which just returns the number of days in the month the date is in. If you're trying to get this month's amount of days, it's even easier. Just running the date() function creates a DateTime set to right now, so the above code becomes:

<?php
$daysInAugust2020 = date('t');
print_r($daysInAugust2020);

Take advantage of the relative formats .

$y_m = '2018-10'; // set year and month.

$list = array();
$d = date('d', strtotime('last day of this month', strtotime($y_m))); // get max date of current month: 28, 29, 30 or 31.

for ($i = 1; $i <= $d; $i++) {
    $list[] = $y_m . '-' . str_pad($i, 2, '0', STR_PAD_LEFT);
}

echo '<pre>';
print_r($list);
echo '</pre>';
$list=array();
$d = 13;
$year = 2019;

for($m=1; $m<=12; $m++)
{
    $time=mktime(12, 0, 0, $m, $d, $year);          
    if (date('m', $time)==$m)       
        $list[]=date('D-d-m-Y', $time);
    }

In this one you can put a spesific number and outpout all the days in the year that have the same number. example i wanted to outpout all the 13th days of the month. (if you want to find every Friday 13th that what you have to use)

This will get all date current month

$alldatethismonth = range(1, $month);
foreach($alldatethismonth as $date){
    echo date("Y-m-").$date."<br>";
}

And this will get all date for given month

$month = date("t", strtotime('2021-09-18'));
$alldatethismonth = range(1, $month);
foreach($alldatethismonth as $date){
    echo date("Y-m-").$date."<br>";
}

This works like a charm. Tried cal_days_in_month but didn't work.

function Get_Month_Dates($year,$month)
{
    $no_of_days = date("t",strtotime($year.'-'.$month));
    
    $dates = array();
    
    for($d=1;$d<=$no_of_days;$d++)
    {
        $day = (strlen($d)==1) ? '0'.$d : $d; // To add leading zero to the date
        $month = (strlen($month)==1) ? '0'.$month : $month; // To add leading zero to the month
        
        $dates[] = $year.'-'.$month.'-'.$day;
    }

    return $dates;
}
$c_year = date("Y");
$c_month = date("m");
$no_day = cal_days_in_month(CAL_GREGORIAN, $c_month, $c_year);           
for($i=1; $i<=$no_day; $i++){ 
     $cd[] .= $c_year.'-'.$c_month.'-'.$i;
}
$date_val = json_encode($cd) 
print_r($date_val); // date array

you may use $list[]=date('YM-D', $time);

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM