繁体   English   中英

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

[英]Get all days and date for a given month

想要检索给定月份的所有日期和日期。 目前有这个显示当月的所有天数,但是我如何在指定的月份进行解析呢?

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

尝试这个

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

尝试这个

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

演示

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

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

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-info.php

对于面向对象的用户,

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

我很惊讶没有人提到 PHP 的内置,没有额外的扩展DateTime类。

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

您必须在YYYYMM字符串中添加一天,但这并不难:每个月都有一个第一天。 所以你总是可以添加01

这里的技巧是t格式,它只返回日期所在月份的天数。如果你想得到这个月的天数,那就更容易了。 只需运行date()函数就会立即创建一个 DateTime 设置,因此上面的代码变为:

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

利用相对格式

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

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

这将获得当前月份的所有日期

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

这将获得给定月份的所有日期

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

这就像一个魅力。 试过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

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

参考

暂无
暂无

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

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