Reputation: 5146
I have a php code as shown below in which I am trying to change calendar after every two months.
What I am trying to achieve on the webapge is:
(a) When the current month is January or February, it should display all dates for January and February (in readonly mode).
(b) When the current month is March or April, it should display all dates for March and April (in readonly mode).
(c) And so on..
The sequence should be January February, March April, May June, July August, September October, November December. (technically the calendar change should happen after every 2 months).
I am also attaching the screenshot in order to make things clear. (I want my webpage to look exactly as it is when the current month is March or April) and if current month is May or June then it should show all dates for those two months only.
Php code:
This is what I have tried but I think more need to be done. I have added foreach
loop for Code A and Code B because it will loop through dates for the month.
The reason why I have added two codes (Code A and Code B) so that one code (Code A) loop through odd month and another code (Code B) loop through even month but I am not sure if that is the way I should implement it.
$odd_months = array('January', 'March', 'May', 'July ', 'September', 'November',);
$current = date('F');
Code A:
<div class="abc-xyz">
<?php foreach ($period as $date) { ?>
<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" readonly="readonly" value="<?php echo $date->format("Y-m-d"); ?>">
</div>
<?php } ?>
</div>
Code B:
<div class="abc-xyz">
<?php foreach ($period as $date) { ?>
<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" readonly="readonly" value="<?php echo $date->format("Y-m-d"); ?>">
</div>
<?php } ?>
</div>
Upvotes: 0
Views: 323
Reputation: 880
You could give this a try. Pass it the number of a month and it should do the rest.
<?php
function buildDateSelection($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 $month) {
# create timestamp for the first of a month
# then get number of days for that month
$numberOfDays = date('t', strtotime($year . '-' . $month . '-01'));
# we can now loop over the days and fill the output string
for($i = 1; $i <= $numberOfDays; $i++) {
$output .= '<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="date" readonly="readonly" value="' . $year . '-' . $month . '-' . str_pad($i, 2, "0", STR_PAD_LEFT) . '">
</div>';
}
}
return $output;
}
?>
Example use cases:
<?php
# specific month
echo buildDateSelection(4);
# get the current month
echo buildDateSelection(date('n'));
?>
Upvotes: 1
Reputation: 131
<div class="abc-xyz"> <?php
$year = date('Y');
$month = date('n');
for($i = $month; $i <= $month+1; $i++) {
$d = cal_days_in_month(CAL_GREGORIAN, $i, $year);
for($j = 1; $j <= $d; $j++){ ?>
<div class="select-date" style="margin-right:30px; margin-top:20px;">
<input type="datetime" readonly="readonly" value="<?php echo $year.'-'.$i.'-'.$j; ?>">
</div> <?php
}
} ?>
</div>
Upvotes: 0