0

My requirement is if I pass an year and month to a php function it must return a 2 dimensional array of dates in that month, most time it will return a 5x7 array (rare occasion when feb 1st is sunday it have only 4x7, 2015 feb for example), each cell of the array must be null if it is from previous month or from next month. Is there any php function to do this ??? enter image description here

8
  • And your question is...? Commented Aug 30, 2017 at 19:13
  • Is there any php function to do this ??? Commented Aug 30, 2017 at 19:14
  • You're going to want to investigate PHP's DateTime class. It should be pretty easy to do this, but if you don't try you won't learn anything. Commented Aug 30, 2017 at 19:19
  • Ok i am trying to figure it out, thanks my friend Commented Aug 30, 2017 at 19:20
  • @Brian Gottier thanks for the motivation as you told it was easy and i have posted an answer to my own question :D Commented Aug 30, 2017 at 19:38

2 Answers 2

1

Just for fun, I did it this way:

<?php

$d = new DateTime('2017 sep');

$last = $d->format('t');

$days[] = array(
    'w' => $d->format('w'),
    'd' => $d->format('Y-m-d')
);

for( $x = 1; $x <= $last -1; $x++ )
{
    $d->modify('+1 day');

    $days[] = array(
        'w' => $d->format('w'),
        'd' => $d->format('Y-m-d')
    );
}

$weeks = array();
$week = 1;
$checked = array();
foreach( $days as $day )
{
    if( $day['w'] != 0 && ! in_array( $week, $checked ) )
    {
        for( $y = 0; $y < $day['w']; $y++ )
        {
            $weeks[$week][$y] = NULL;
        }
    }

    $checked[] = $week;

    $weeks[$week][$day['w']] = $day['d'];

    if( $day['w'] == 6 )
        $week++;
}

$weeks_count = count( $weeks );
$last_week_count = count( $weeks[$weeks_count] );
if( $last_week_count != 7 )
{
    $cells_needed = 7 - $last_week_count;
    for( $x = 6; $x > $cells_needed +1; $x--)
    {
        $weeks[$weeks_count][$x] = NULL;
    }
}

echo '<pre>';
print_r( $weeks );
echo '</pre>';
Sign up to request clarification or add additional context in comments.

2 Comments

This is not I wanted, because when i changed to $d = new DateTime('2015 may'); it shows all the dates but the last 6 null which is junes first 6 days is not showing up plz correct it, but my answer works perfectly
You have your correct answer, based on your original question and comments. Don't make me jump through hoops for nothing.
0

After some research i figured that php doesn't have any built in function to do this, so i created one to do this, hope this help some one else

<?php
   function getMonthDaysBlock($year,$month,$show_weeks = true) {

        $num = cal_days_in_month(CAL_GREGORIAN, $month, $year);
        $dates_month = array();

        $first_day = mktime(0, 0, 0, $month, 1, $year);
        $remain_prev_days = date('N', $first_day) + 1;
        if($remain_prev_days == 8)$remain_prev_days = 1;
        $remain_prev_days--;

        $last_day = mktime(0, 0, 0, $month, $num, $year);
        $remain_next_days = date('N', $last_day) + 1;
        if($remain_next_days == 8)$remain_next_days = 1;
        $remain_next_days = 7 - $remain_next_days;

        for ($i=0; $i < $remain_prev_days; $i++) { 
            array_push($dates_month, NULL);
        }
        for ($i = 1; $i <= $num; $i++) {
            $mktime = mktime(0, 0, 0, $month, $i, $year);
            $date = date("Y-m-d", $mktime);
            array_push($dates_month, $date);
        }
        for ($i=0; $i < $remain_next_days; $i++) { 
            array_push($dates_month, NULL);
        }

        return getMonthArray($dates_month,$show_weeks);

    }
    function getMonthArray($days,$show_weeks = true)
    {
        $weeks = ['SUN','MON','TUE','WED','THU','FRI','SAT'];
        $newArray = [];
        $count = count($days);
        $iMax = (int)($count/7);
        $jMax = 7;
        $incr = 0;

        for ($i=0; $i < $iMax; $i++) 
        { 
            for ($j=0; $j < $jMax; $j++) 
            { 
                if($show_weeks)
                    $newArray[$i][$weeks[$j]] = $days[$incr];
                else
                    $newArray[$i][$j] = $days[$incr];
                $incr++;
            }
        }

        return $newArray;
    }


var_dump(getMonthDaysBlock(2015,05));
?>

2 Comments

Doesn't show the 31st of the month for the month you have decided to demonstrate.
It was just a small error while copy pasting the code from editor, corrected it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.