Jump to content

Recommended Posts

Hi Guys,

 

In a form within PHP coding i can get the the next 10 years with the following code:

 

<?php 
$date_future = date("Y", strtotime('+10 year'));
$date_year = date("Y");
for($i=$date_year;$i<$date_future;$i++){
if($date_year == $i){ 
echo "<option value=\"$i\" selected=\"selected\">$i</option> \n";
} else { 
echo "<option value=\"$i\">$i</option> \n"; 
}
} 
?>

 

I have tried to mess about with the same code and try and get it to work for the last 10 years without any luck. He's what i did:

<?php 
$date_past = date("Y", strtotime('-10 year')); 
$date_year = date("Y");
for($i=$date_year;$i>$date_past;$i++){
if($date_year == $i){ 
echo "<option value=\"$i\" selected=\"selected\">$i</option> \n"; 
} else { 
echo "<option value=\"$i\">$i</option> \n"; 
}
} 
?>

 

I would appreciate any help with this.

Link to comment
https://forums.phpfreaks.com/topic/245845-get-last-10-years-in-form/
Share on other sites

Hi, thanks for replying. I've been trying similar ways to what you showed but none work and neither dies your code as I get either a blank page or a empty year select box (when trying to get the previous 10 years, starting this year).

 

Tho, it shouldn't be that complicated as my first code works for getting the future 10 years.

A couple of ways to do it . . .

 

// START WITH THIS //
echo "<select name=\"year\">\n";
$now = date('Y');
$then = $now - 10;

// THEN USE WITH THIS //
$years = range( $now, $then );
foreach( $years as $v ) {
echo "<option value=\"$v\">$v</option>\n";
}

// OR THIS //
for( $i = $now; $i >= $then; $i-- ) {
echo "<option value=\"$i\">$i</option>\n";
}

// THEN CLOSE THE FORM ELEMENT //
echo "</select>\n";

I prefer not to have hard-set values for the number of options to show. This should work as well and you can easily set the number fo years back to show by setting the first variable accordingly

 

$years_to_show = 10;

$start_year = date('Y');
for($offset=0; $offset<$years_to_show; $offset++)
{
    $year = $start_year - $offset;
    echo "<option value=\"{$year}\">{$year}</option>\n";
}

Thanks guys, especially Pikachu2000. After studying his code i realised that the only reason my code would not work is because i used the code:

$i++

 

in the line:

for($i=$date_year;$i>$date_past;$i++){

 

When all i had to change it to was:

$i--

 

Whats the meaning of that? I never had to use it before and always used $i++ or ++$i. It's best to know !! :)

Plus, there is a significant difference between $i-- and --$i

 

Per the manual (emphasis added): http://php.net/manual/en/language.operators.increment.php

 

++$a    Pre-increment    Increments $a by one, then returns $a.

$a++    Post-increment    Returns $a, then increments $a by one.

--$a    Pre-decrement    Decrements $a by one, then returns $a.

$a--    Post-decrement    Returns $a, then decrements $a by one.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.