问题
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>";
回答1:
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>";
回答2:
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
回答3:
$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
回答4:
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;
}
回答5:
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>';
回答6:
$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)
回答7:
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);
回答8:
$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
回答9:
you may use $list[]=date('Y-M-D', $time);
Reference
来源:https://stackoverflow.com/questions/23780293/get-all-days-and-date-for-a-given-month