Get all days and date for a given month

后端 未结 9 1526
天涯浪人
天涯浪人 2021-02-10 08:17

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?<

相关标签:
9条回答
  • 2021-02-10 08:41
    $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)

    0 讨论(0)
  • 2021-02-10 08:45

    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;
    }
    
    0 讨论(0)
  • 2021-02-10 08:50

    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>';
    
    0 讨论(0)
  • 2021-02-10 08:51

    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);
    
    0 讨论(0)
  • 2021-02-10 08:53
    $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

    0 讨论(0)
  • 2021-02-10 08:53
    $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
    
    0 讨论(0)
提交回复
热议问题