skip to Main Content

How can I get list of dates of the current month till current date of that month?The Code I have tried so far. I am getting all the dates of this current month.


$month = 12;
$year = 2022;
   for($d=1; $d<=31; $d++)
            {
                $time = mktime(12, 0, 0, $month, $d, $year); 

                if (date('m', $time) == $month)  

                    $list[]=date('Y-m-d', $time);
            }

4

Answers


  1. You can try like this:

    $dates = [];
    for($i = 1; $i <=  date('t'); $i++)
    {
        $date = date('Y') . "-" . date('m') . "-" . str_pad($i, 2, '0', STR_PAD_LEFT);
        if(strtotime($date) > strtotime(date('Y-m-d'))) {
            break;
        }
        $dates[] = $date;
    }
    
    var_dump($dates);
    
    Login or Signup to reply.
  2. This answer works for OP’s original question ("current month") not for the edited answer check the marked as answer one.

    
    $list = [];
    
    $now_day = intval(date('d'));
    
    for($d=1; $d<=$now_day; $d++)
    {
        $list[]= date('Y-m-').str_pad($d, 2, "0", STR_PAD_LEFT);
    }
    
    
    
    Login or Signup to reply.
  3. There’s no need to compare anything. It’s sufficient to generate a range of days from 1 until today (if current year/month), or the end of the month (custom year/month), and then then iterate over it, appending days (zero-padded) to the Y-m- base date. As follows:

    /* If for current year/month */
    $base = date('Y-m'); // Current base date
    $days = range(1,  date('d')); // Days from 1 to now
    
    /* If for other year/month */
    // $base = '1980-02'; // Literal base date
    // $days = range(1,  date('t', strtotime($base))); // Days from 1 to  month max
    
    /* Combine with day added */
    $dates = array_map(function($day) use ($base) {
        return $base . '-' . str_pad((string) $day, 2, "0", STR_PAD_LEFT);
    }, $days);
    

    If generating dates for this year/month, use the $base = date('Y-m'); date call . For other year/month, use the literal $base = '1980-02'; and generate the day range to match the days in that month. With date('t') to get "days in month", leap years are considered.

    The first option produces (as of today) the following array:

    [
      0 => '2022-12-01',
      1 => '2022-12-02',
      2 => '2022-12-03',
      3 => '2022-12-04',
      4 => '2022-12-05',
      5 => '2022-12-06',
      6 => '2022-12-07',
      7 => '2022-12-08',
      8 => '2022-12-09',
      9 => '2022-12-10',
      10 => '2022-12-11',
      11 => '2022-12-12',
      12 => '2022-12-13',
      13 => '2022-12-14',
      14 => '2022-12-15',
    ]
    

    Note the str_pad((string) $day in padding days, where we typecast the $day number to a string. Why? We are iterating integers, while str_pad() expects a string. If the argument is not a string, a TypeError will be thrown when coding with declare(strict_types=1) on.


    The above wrapped in a function to make this convenient. See Demo, Source.

    /**
     *  Get Dates In Month.
     *  
     *  @updated 2022-12-15 19:09:31 +07:00
     *
     *  @param bool|string  $yearmonth
     *                      true = current year/month
     *                      string = other year/month
     *  
     *  @note If using PHP <8, remove the bool|string union type signature!
     *  @source https://bitbucket.org/cmswares/stack_overflow/src/master/functions/func.get_dates_in_month.php
     *  
     *  @return array
     */
    
    function get_dates_in_month(bool|string $yearmonth): array
    {   
        /* If for current year/month */
        if($yearmonth === true) {
            $base = date('Y-m'); // Current base date
            $days = range(1,  date('d')); // Days from 1 to now
        } 
        /* If for other year/month */
        else {
            $base = $yearmonth; // Literal base date
            $days = range(1,  date('t', strtotime($yearmonth))); // Days from 1 to  month max
        }
    
        /* Combine with day added */
        $dates = array_map(function($day) use ($base) {
            return $base . '-' . str_pad((string) $day, 2, "0", STR_PAD_LEFT);
        }, $days);
        return $dates;
    }
    
    echo "==== Current Y/M ====n";
    var_dump(get_dates_in_month(true));
    
    echo "==== Other Y/M ====n";
    var_dump(get_dates_in_month('1980-02'));
    
    Login or Signup to reply.
  4. This also should work.

    $date = '2022-12-01'; //you can choose any date here
    $end = '2022-12-' . date('t', strtotime($date));
    
    while(strtotime($date) <= strtotime($end)) {
        $day_num = date('d', strtotime($date));
        $day_month = date('m', strtotime($date));
        $day_name = date('l', strtotime($date));
        $day_year = date('y', strtotime($date));
        $date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
        echo "{$day_name} {$day_num} {$day_month} {$day_year}" . PHP_EOL;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search