mhouldridge
asked on
Date increment not working?
Experts,
I have the following code to increment the dates within a selection box;
<?php
$start_date = time() - (720 * 24 * 60 * 60); // today's date
$end_date = strtotime('+2 years'); // Two years from today's date
echo '<select name="dateone"><option>Cho ose One</option>'."\n";
for ($i = 1, $date = $start_date; $date <= $end_date; $i++) {
$option_date = date('d-m-Y', $date);
echo '<option value="'.$option_date.'">' .$option_d ate.'</opt ion>'."\n" ;
$date = $date + ($i*24*60*60);
}
echo '</select>';
?>
Problem is, the increment does not work correctly and I cant figure out why?
I have the following code to increment the dates within a selection box;
<?php
$start_date = time() - (720 * 24 * 60 * 60); // today's date
$end_date = strtotime('+2 years'); // Two years from today's date
echo '<select name="dateone"><option>Cho
for ($i = 1, $date = $start_date; $date <= $end_date; $i++) {
$option_date = date('d-m-Y', $date);
echo '<option value="'.$option_date.'">'
$date = $date + ($i*24*60*60);
}
echo '</select>';
?>
Problem is, the increment does not work correctly and I cant figure out why?
How do you want it to work?
What kind of results are you looking for?
What kind of results are you looking for?
ASKER
Sorry,
I need to go back 2 years, and then move forward until the latest date is something like 2010. This line was commented incorrectly, apologies;
$start_date = time() - (720 * 24 * 60 * 60); // today's date
should read;
$start_date = time() - (720 * 24 * 60 * 60); // ********* GO back 2 years ***********
The result is really strange. The first date is retrieved, but the rest start from today's date.
I need to go back 2 years, and then move forward until the latest date is something like 2010. This line was commented incorrectly, apologies;
$start_date = time() - (720 * 24 * 60 * 60); // today's date
should read;
$start_date = time() - (720 * 24 * 60 * 60); // ********* GO back 2 years ***********
The result is really strange. The first date is retrieved, but the rest start from today's date.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sorry this should work
<?php
$start_date = strtotime('-2 years'); // today's date
$end_date = strtotime('+2 years'); // Two years from today's date
echo '<select name="dateone"><option>Cho ose One</option>'."\n";
for ($i = 1, $date = $start_date; $date <= $end_date; $i++) {
$option_date = date('d-m-Y', $date);
echo '<option value="'.$option_date.'">' .$option_d ate.'</opt ion>'."\n" ;
$date = strtotime('+'.$i.' days',$start_date) ;
}
echo '</select>';
?>
<?php
$start_date = strtotime('-2 years'); // today's date
$end_date = strtotime('+2 years'); // Two years from today's date
echo '<select name="dateone"><option>Cho
for ($i = 1, $date = $start_date; $date <= $end_date; $i++) {
$option_date = date('d-m-Y', $date);
echo '<option value="'.$option_date.'">'
$date = strtotime('+'.$i.' days',$start_date) ;
}
echo '</select>';
?>
ASKER
Fixed it now,
Here is my code;
<?php
echo "<select name='startdate' id='startdate'>";
$start_date = time() - (720 * 24 * 60 * 60); // today's date
$date = date("d-m-Y", $start_date);
$future_date = time() + (1800 * 24 * 60 * 60); // today's date
for ($i = 1, $loop_date = $start_date; $loop_date < $future_date; $i++){
$loop_date = $start_date + ($i*24*60*60);
$date_formatted = date("d-m-Y", $loop_date);
$SQL_date = date("Y-m-d", $loop_date);
echo "<option value='".$SQL_date."'>".$d ate_format ted."</opt ion>\n";
}
echo "</select>";
?>
Here is my code;
<?php
echo "<select name='startdate' id='startdate'>";
$start_date = time() - (720 * 24 * 60 * 60); // today's date
$date = date("d-m-Y", $start_date);
$future_date = time() + (1800 * 24 * 60 * 60); // today's date
for ($i = 1, $loop_date = $start_date; $loop_date < $future_date; $i++){
$loop_date = $start_date + ($i*24*60*60);
$date_formatted = date("d-m-Y", $loop_date);
$SQL_date = date("Y-m-d", $loop_date);
echo "<option value='".$SQL_date."'>".$d
}
echo "</select>";
?>
the code i have above works ok 2 years back and 2 years in the future .
With code you just posted i get first date 11-11-2005 should be 31-10-2005 and end date 04-12-2012 where it should be 31-10-2009.
Or thats how i understood it .
With code you just posted i get first date 11-11-2005 should be 31-10-2005 and end date 04-12-2012 where it should be 31-10-2009.
Or thats how i understood it .
$start_date = time(); // today's date
$end_date = strtotime('+2 years'); // Two years from today's date
echo '<select name="dateone"><option>Cho
for ($i = 1, $date = $start_date; $date <= $end_date; $i++) {
$option_date = date('d-m-Y', $date);
echo '<option value="'.$option_date.'">'
$date = strtotime('+'.$i.' days');
}
echo '</select>';
?>