Jump to content

[SOLVED] Looping date problem


rvdb86

Recommended Posts

I really hope this is not a case of working with code for the whole day and I have failed to spot an obvious mistake but I am trying to out put a list of the next 10 years (eg 2009, 2010....2018, 2019)

 

heres my code:

<?php
				$y = date('Y');
				$l = 1;
				while ($l <= 10){
				?>
                    <option value="<?php echo $y; ?>"><?php echo $y; ?></option>
                    <?php
				$plusOne = strtotime(date("Y", strtotime($y)) . " +1 year");
				$y = date('Y', $plusOne);
				$l++;
				}
				?>

 

It only increases the date to from 2009 to 2010 and then repeats 2010 nine times  :shrug:

 

TIA for any suggestions!

Link to comment
https://forums.phpfreaks.com/topic/179515-solved-looping-date-problem/
Share on other sites

Your $plusOne variable is not increasing, therefore returning the same result each time. You will need to change the +1 year to include the $l variable...

 

<?php
				$y = date('Y');
				$l = 1;
				while ($l <= 10){
				?>
                    <option value="<?php echo $y; ?>"><?php echo $y; ?></option>
                    <?php
				$plusOne = strtotime(date("Y", strtotime($y)) . " +$l years");
				$y = date('Y', $plusOne);
				$l++;
				}
				?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.