Link to home
Start Free TrialLog in
Avatar of mhouldridge
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>Choose 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_date.'</option>'."\n";
    $date =  $date + ($i*24*60*60);
}

echo '</select>';
?>

Problem is, the increment does not work correctly and I cant figure out why?
Avatar of steelseth12
steelseth12
Flag of Cyprus image

<?php

$start_date = time(); // today's date
$end_date = strtotime('+2 years'); // Two years from today's date

echo '<select name="dateone"><option>Choose 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_date.'</option>'."\n";
   $date =  strtotime('+'.$i.' days');
}

echo '</select>';
?>
How do you want it to work?

What kind of results are you looking for?
Avatar of mhouldridge
mhouldridge

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.
ASKER CERTIFIED SOLUTION
Avatar of steelseth12
steelseth12
Flag of Cyprus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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>Choose 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_date.'</option>'."\n";
   
   
   
   $date =  strtotime('+'.$i.' days',$start_date) ;
   
   
}

echo '</select>';
?>
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."'>".$date_formatted."</option>\n";
}

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 .