• Status: Solved
• Priority: Medium
• Security: Public
• Views: 285

# Calculate Date Help

Hi,

This function calculates 1 year head of dates. How do i make it calculate 1 year before and 1 year after now.

eg: May 2005 - May 2007 in the selection

<?
for (\$i = 0; \$i < 15; \$i++) {
\$thisMonth = (\$month + \$i) % 12;
\$thisYear = \$year + (\$month + \$i) / 12;
?>
<option value="<? _e(date("m/Y", mktime(0, 0, 0, \$thisMonth, 1, \$thisYear))); ?>"><?
_e(date("M Y", mktime(0, 0, 0, \$thisMonth, 1, \$thisYear))); ?></option>
<?
0
jedistar
• 4
• 3
1 Solution

Commented:
<?php

// get our start time (a year ago), and put
// it in the middle of the month, just to be sure
\$date = strtotime('-1 year');
\$date = strtotime(date('Y-m-10', \$date));

// get our end time (a year from now), and
// put it at the end of the month
\$end = strtotime('+1 year');
\$end = strtotime(date('Y-m-t', \$end));

while (\$date <= \$end)
{
echo '<option value="'.date('m/Y', \$date).'">'.date('M Y', \$date).'</option>'."\n";
\$date = strtotime('+1 month', \$date);
}

?>
0

Author Commented:
Ok, currently my date displays as:

<input id="holiday_date" name="holiday_date" type="text" tabindex="2" <?
if (array_key_exists ('holiday_date', \$cleanPost))
_e(" value=\"{\$cleanPost['holiday_date']}\"");
else if (\$currentQuery)
_e(" value=\"" . \$currentQuery->date . "\"");
?>/>

eg: its 2006-01-02 in a texbox format, how do i change it to the format of:

<select id="holiday_day" name="holiday_day" class="f-day" tabindex="4">
<?
for (\$i = 1; \$i < 32; \$i++) {
?>
<option value="<? _e(\$i); ?>"<?if(\$i == \$day) _e(" selected")?>><? _e(\$i); ?></option>
<?
}
?>
</select>
<select id="holiday_monthyear" name="holiday_monthyear" class="f-month" tabindex="5">
<?
for (\$i = 0; \$i < 15; \$i++) {
\$thisMonth = (\$month + \$i) % 12;
\$thisYear = \$year + (\$month + \$i) / 12;
?>
<option value="<? _e(date("m/Y", mktime(0, 0, 0, \$thisMonth, 1, \$thisYear))); ?>"><?
_e(date("M Y", mktime(0, 0, 0, \$thisMonth, 1, \$thisYear))); ?></option>
<?
}
?>
</select>

(points changed to 500)
0

Author Commented:
you there?
0

Commented:
You may want to clarify what you're wanting, I only have a vague idea of what your asking, not nearly enough to try to help.
0

Author Commented:
my previous post:

currently my date displays in a textbox, i wish to display it in a form on 2 combobox
day-monthyear -> with the current date selected. the user should be allowed to
select a range of dates +/-1year
0

Commented:

<select name="dates">
<?php
\$month = date("m");
\$year = date("Y");
for(\$i = 12; \$i > -13; \$i--)
{
\$date = date("m/Y", mktime(0,0,0, \$month-\$i, 1, \$year));
echo "\t<option value=\"\$date\">\$date\n";
}
?>
</select>
0

Author Commented:
how do i split up my current date first
0

Commented: