简体   繁体   English

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

[英]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. cal_days_in_month:返回给定年份和日历的一个月中的天数。
First parameter is "calendar":第一个参数是“日历”:

0 or CAL_GREGORIAN - Gregorian Calendar 0 或 CAL_GREGORIAN - 公历
1 or CAL_JULIAN - Julian Calendar 1 或 CAL_JULIAN - 儒略历
2 or CAL_JEWISH - Jewish Calendar 2 或 CAL_JEWISH - 犹太历
3 or CAL_FRENCH - French Revolutionary Calendar 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

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 的内置,没有额外的扩展DateTime类。

<?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.您必须在YYYYMM字符串中添加一天,但这并不难:每个月都有一个第一天。 So you can always add 01 .所以你总是可以添加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.这里的技巧是t格式,它只返回日期所在月份的天数。如果你想得到这个月的天数,那就更容易了。 Just running the date() function creates a DateTime set to right now, so the above code becomes:只需运行date()函数就会立即创建一个 DateTime 设置,因此上面的代码变为:

<?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.例如,我想输出该月的所有第 13 天。 (if you want to find every Friday 13th that what you have to use) (如果你想在每个星期五 13 日找到你必须使用的东西)

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.试过cal_days_in_month但没有用。

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);你可以使用$list[]=date('YM-D', $time);

Reference参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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