I have a php code as shown below in which I want to show current and next month dates in php. Below are the two functions which I am using in order to show current and next month dates.
html/php code (current month function and next month function):
<!-- current month function START -->
<?php
function buildDateSelectionCurrentMonth($month)
{
# array will hold our months
$months = [
str_pad($month, 2, "0", STR_PAD_LEFT),
str_pad(($month + 1), 2, "0", STR_PAD_LEFT),
];
# will hold our HTML
$output = '';
# got our months now, let's iterate over them
foreach ($months as $index => $month) {
$year = date('Y');
// Adjust month and date if necessary:
if ($month == 13) {
$month = 1;
$year++;
}
if ($index == 0) {
# create timestamp for the first of a month
# then get number of days for that month
$numberOfDaysMonth = date('t', strtotime($year . '-' . $month . '-01'));
# we can now loop over the days and fill the output string
for ($i = 1; $i <= $numberOfDaysMonth; $i++) {
$output .= '<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" class="class-sitting-date" name="class_sitting_date_current_month[]" readonly="readonly" value="' . $year . '-' . $month . '-' . str_pad($i, 2, "0", STR_PAD_LEFT) . '">
</div>';
}
}
}
return $output;
}
<!-- current month function END -->
<!-- next month function START -->
<?php
function buildDateSelectionNextMonth($month)
{
# array will hold our months
$months = [
str_pad($month, 2, "0", STR_PAD_LEFT),
str_pad(($month + 1), 2, "0", STR_PAD_LEFT),
];
# will hold our HTML
$output = '';
# got our months now, let's iterate over them
foreach ($months as $index => $month) {
$year = date('Y');
// Adjust month and date if necessary:
if ($month == 13) {
$month = 1;
$year++;
}
if ($index == 0) {
# create timestamp for the first of a month
# then get number of days for that month
$numberOfDaysMonth = date('t', strtotime($year . '-' . $month . '-01'));
# we can now loop over the days and fill the output string
for ($i = 1; $i <= $numberOfDaysMonth; $i++) {
$output .= '<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" class="class-sitting-date" name="class_sitting_date_next_month[]" readonly="readonly" value="' . $year . '-' . $month . '-' . str_pad($i, 2, "0", STR_PAD_LEFT) . '">
</div>';
}
}
}
return $output;
}
?>
<!-- next month function END -->
html/php code (current month dates and next month dates):
<!-- Current Month Dates START -->
<div class="sitting-days">
<h4 name="dates-selection" style="text-align:center;"><a name="dates-selection">Select Date</a>
</h4>
<?php echo buildDateSelectionCurrentMonth(date('n')); ?> // Line Y
</div>
<!-- Current Month Dates END -->
<!-- Next Month Dates START -->
<div class="sitting-days" style="margin-left:30px;">
<h4 style="text-align:center;">Select Date</h4>
<div class="sitting-days">
<?php echo buildDateSelectionNextMonth(date('n')+1); ?> // Line Z
</div>
</div>
<!-- Next Month Dates END -->
At Line Y and Line Z its currently displaying November and December dates
When the current month is September then it will display September 2020 dates and October 2020 dates and vice-versa.
Problem Statement:
Just for fun, at Line Y and Line Z I did the following changes:
At Line Y:
echo buildDateSelectionCurrentMonth(12);
At Line Z
echo buildDateSelectionNextMonth(13);
The reason why I did that because I want to check whether it will display January 2021 dates or not.
I noticed that it didn't display January 2021 dates at Line Z although it was displaying December 2020 dates at Line Y.
I am wondering what changes I need to make in the php code above so that it displays January 2021 dates
when the current month is December
.
My Answer:
php
function getCurrentMonthDates() {
$currentMonth = date('m');
$currentYear = date('Y');
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
$currentMonthDates = array();
for ($i = 1; $i <= $daysInMonth; $i++) {
$currentMonthDates[] = $currentYear . '-' . $currentMonth . '-' . str_pad($i, 2, '0', STR_PAD_LEFT);
}
return $currentMonthDates;
}
function getNextMonthDates() {
$nextMonth = date('m', strtotime('+1 month'));
$nextYear = date('Y', strtotime('+1 month'));
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $nextMonth, $nextYear);
$nextMonthDates = array();
for ($i = 1; $i <= $daysInMonth; $i++) {
$nextMonthDates[] = $nextYear . '-' . $nextMonth . '-' . str_pad($i, 2, '0', STR_PAD_LEFT);
}
return $nextMonthDates;
}
$currentMonthDates = getCurrentMonthDates();
$nextMonthDates = getNextMonthDates();
?>
Current and Next Month Dates
Current Month Dates:
Next Month Dates:
Rate this post
3 of 5 based on 8448 votesComments