Solved

Calculate Date Help

Posted on 2006-05-21
230 Views
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
Question by:jedistar

LVL 5

Expert Comment

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

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 Comment

you there?
0

LVL 6

Expert Comment

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 Comment

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

LVL 6

Expert Comment

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

how do i split up my current date first
0

LVL 6

Accepted Solution

\$current_date = "05-18-2006";
you could do something like

list (\$month, \$day, \$year) = explode("-", \$current_date);
0

Featured Post

This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.