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

-
And your question is...?iquellis– iquellis2017-08-30 19:13:38 +00:00Commented Aug 30, 2017 at 19:13
-
Is there any php function to do this ???Ananthakrishnan Baji– Ananthakrishnan Baji2017-08-30 19:14:45 +00:00Commented 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.Brian Gottier– Brian Gottier2017-08-30 19:19:12 +00:00Commented Aug 30, 2017 at 19:19
-
Ok i am trying to figure it out, thanks my friendAnanthakrishnan Baji– Ananthakrishnan Baji2017-08-30 19:20:07 +00:00Commented 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 :DAnanthakrishnan Baji– Ananthakrishnan Baji2017-08-30 19:38:26 +00:00Commented Aug 30, 2017 at 19:38
|
Show 3 more comments
2 Answers
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>';
2 Comments
Ananthakrishnan Baji
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 perfectlyBrian Gottier
You have your correct answer, based on your original question and comments. Don't make me jump through hoops for nothing.
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
Brian Gottier
Doesn't show the 31st of the month for the month you have decided to demonstrate.
Ananthakrishnan Baji
It was just a small error while copy pasting the code from editor, corrected it