Reputation: 5146
I have a php code as shown below in which the calendar changes after every two months.
(a) When the current month is January or February
, it displays all the dates for January
and February
(in readonly mode).
(b) When the current month is March or April
, it displays all the dates for March
and April
(in readonly mode).
(c) And so on..
php odd month and even month functions:
<!-- odd month -->
<?php
function buildDateSelectionOdd($month)
{
# array will hold our months
$months = array();
# determine whether we have an odd or even month number
if ($month % 2 == 0) {
# even month is always the second
# so add the one before $month and $month itself
$months[] = str_pad(($month - 1), 2, "0", STR_PAD_LEFT);
$months[] = str_pad($month, 2, "0", STR_PAD_LEFT);
} else {
# for odd ones add $month and $month + 1
$months[] = str_pad($month, 2, "0", STR_PAD_LEFT);
$months[] = str_pad(($month + 1), 2, "0", STR_PAD_LEFT);
}
# will hold our HTML
$output = '';
# got our months now, let's iterate over them
$year = date('Y');
foreach ($months as $index => $month) {
if ($index == 0) {
echo "Index 0 is ";
print_r($month);
# create timestamp for the first of a month
# then get number of days for that month
$numberOfDaysOddMonth = date('t', strtotime($year . '-' . $month . '-01'));
# we can now loop over the days and fill the output string
for ($i = 1; $i <= $numberOfDaysOddMonth; $i++) {
$output .= '<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" class="house-sitting-date-odd-month" name="house_sitting_date_odd_month[]" readonly="readonly" value="' . $year . '-' . $month . '-' . str_pad($i, 2, "0", STR_PAD_LEFT) . '">
</div>';
}
}
}
return $output;
}
?>
<!-- even month -->
<?php
function buildDateSelectionEven($month)
{
# array will hold our months
$months = array();
# determine whether we have an odd or even month number
if ($month % 2 == 0) {
# even month is always the second
# so add the one before $month and $month itself
$months[] = str_pad(($month - 1), 2, "0", STR_PAD_LEFT);
$months[] = str_pad($month, 2, "0", STR_PAD_LEFT);
} else {
# for odd ones add $month and $month + 1
$months[] = str_pad($month, 2, "0", STR_PAD_LEFT);
$months[] = str_pad(($month + 1), 2, "0", STR_PAD_LEFT);
}
# will hold our HTML
$output = '';
# got our months now, let's iterate over them
$year = date('Y');
foreach ($months as $index => $month) {
if ($index == 1) {
echo "Index 1 is ";
print_r($month);
# create timestamp for the first of a month
# then get number of days for that month
$numberOfDaysEvenMonth = date('t', strtotime($year . '-' . $month . '-01'));
# we can now loop over the days and fill the output string
for ($i = 1; $i <= $numberOfDaysEvenMonth; $i++) {
$output .= '<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" class="house-sitting-date-even-month" name="house_sitting_date_even_month[]" readonly="readonly" value="' . $year . '-' . $month . '-' . str_pad($i, 2, "0", STR_PAD_LEFT) . '">
</div>';
}
}
}
return $output;
}
?>
html code calling odd and even month functions:
<!-- Odd 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 buildDateSelectionOdd(date('n')); ?>
</div>
<!-- Odd Month Dates END -->
<!-- Even 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 buildDateSelectionEven(date('n')); ?>
</div>
</div>
<!-- Even Month Dates END -->
The above html code display all the dates for the odd and even month as shown in the screenshot below:
What I want to achieve is I want to display the current (August) and next (September) month dates.
1. When the current month is January
then the webpage should display January
and February
dates.
2. When the current month is February
then the webpage should display February
and March
dates.
3. And so on..
I am wondering what changes I should make in the odd/even functions and the html code above so that it shows the current month and next month dates on the webpage meaning the screenshot should show August
and September
dates.
Upvotes: 0
Views: 1265
Reputation: 9273
Add $month
and $month+1
to the array. Then, when you're looping through the array of months, if you're at month 13 then turn that into 1 (January) and look at $year+1
:
function buildDateSelectionOdd($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) {
echo "Index 0 is ";
print_r($month);
# create timestamp for the first of a month
# then get number of days for that month
$numberOfDaysOddMonth = date('t', strtotime($year . '-' . $month . '-01'));
# we can now loop over the days and fill the output string
for ($i = 1; $i <= $numberOfDaysOddMonth; $i++) {
$output .= '<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" class="house-sitting-date-odd-month" name="house_sitting_date_odd_month[]" readonly="readonly" value="' . $year . '-' . $month . '-' . str_pad($i, 2, "0", STR_PAD_LEFT) . '">
</div>';
}
}
}
return $output;
}
Upvotes: 1
Reputation: 10627
You should use DateTime
since it will automatically factor dates based on a DateIneterval passed to it's ->add
or ->sub
method. The interval passed is a String that starts with P
for period, followed by how many and 'M'
, 'D'
, or 'Y'
, for most cases.
<?php
date_default_timezone_set('America/Vancouver'); // set PHP timezone before headers
$date = new DateTime; $thisMonth = $date->format('F');
$nextMonth = $date->add(new DateInterval('P1M'))->format('F');
?>
Upvotes: 0