php 获取给定月份的所有日期和日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23780293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:56:41  来源:igfitidea点击:

Get all days and date for a given month

phparraysdate

提问by user3543512

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

回答by fortune

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

回答by Satish Sharma

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

演示

回答by Tristan

$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":

cal_days_in_month:返回给定年份和日历一个月中的天数。
第一个参数是“日历”:

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

0 或 CAL_GREGORIAN - 公历
1 或 CAL_JULIAN - 儒略历
2 或 CAL_JEWISH - 犹太历
3 或 CAL_FRENCH - 法国革命历

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

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

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

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

回答by Pomanh

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

回答by nggit

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

回答by niquole

$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)

在这个中,您可以输入一个特定的数字并输出一年中具有相同数字的所有天数。例如,我想输出该月的所有第 13 天。(如果你想在每周五 13 日找到你必须使用的东西)

回答by henis

$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

回答by SagarPPanchal

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

你可以使用 $list[]=date('Y-M-D', $time);

Reference

参考