PHP Appending year the select menu - php

I have this custom function in place, It will get the current year from the date function in php. When next year will come I want to append next year with previous year.
Example:
Current Year is "2013"
Next year will be "2014"
When year will come, It should dynamically add 2014 to drop-down list.
<select>
<option>2014</option>
<option>2013</option>
</select>
function ec_year(){
$year = date("Y");
}
Anyone any help please.

Use this :
$start_year = '2013';
$current_year = date("Y");
echo '<select>';
for ($i=$start_year; $i <= $current_year; $i++) {
echo '<option>' . $i .'</option>';
}
echo '</select>';

Try this give the end year as you like ...it will automatically work on present year
<select>
<?php for($i=date(Y);$i>=endyear);$i--){
<option><?php echo $i;?></option>
<?php }?></select>

Try this:
<select>
<option><?php print(Date("Y") + 1); ?></option>
<option><?php print(Date("Y")); ?></option>
</select>

Try:
echo date('Y')+1; #will give current year + 1

Related

PHP - Setting default year in selection box

I am new to PHP. I have a selection box with a range of years starting at current year - 5 (2014) through the year 2050. My selection box contains the correct years but I need it to default to the current year. Right now, it defaults to 2014. I've been working on this for hours. I thought the $current_year variable was needed in the loop in some way - in order to set the default - but that doesn't work. After researching and seeing various examples, I'm now thinking I need to build an if statement within the loop but I really don't know.
<form id="calendarForm" name="calendarForm" method="post" action="">
<label for="select_year">Select the year: </label>
<select id="select_year" name="select_year">
<?php
$date = new DateTime();
// Sets the current year
$current_year = $date->format('Y');
// Year to start available options.
$earliest_year = ($current_year - 5);
$year = $earliest_year;
for ($year = $earliest_year; $year <= LATEST_YEAR; $year++) {
echo "<option value='$year'>$year</option>";
}
?>
</select>
Modify your for loop as follows:
for ($year = $earliest_year; $year <= LATEST_YEAR; $year++) {
if($year==$current_year)
{
echo "<option value='$year' selected>$year</option>";
}
else{
echo "<option value='$year'>$year</option>";
}
}
Try the following. You need to adjust your php tags but neat.
<option value='$year' <?php if($year == date("Y")) {echo "selected";}?> >$year</option>";
It will check against the current year and will be the selected option if matched.

How do I get the year value of the selected option with the present year in PHP?

I have a combobox of year options with 2 year interval. If the user selects the one with the present year which is 2014-2016 (because 2016 is the present year). I want to get or know that the user's option is the one with the current year. Don't mind about the rest option, but just the one with the present year because I'm gonna use it in another condition. Help please.
<select name="year">
<option>Year:</option>
<?php
for($i=date('Y'); $i>1999; $i=$i-2) {
$selected = '';
$year2 = $i-2;
if ($year == $i) $selected = ' selected="selected"';
echo ('<option value="'.$year2. " - " . $i .'" '.$selected.'> '.$year2.' - '.$i.'</option>'."\n");
}
?>
</select>

PHP Loop for Year Dropdown

I am trying to create a HTML select input based on months/years and I am not sure the best approach for it.
While creating a standard loop of years was the first place I started, I wasn't sure how to get the last days of each of the month/years in order to create the values.
Is there any built in PHP functions that can accomplish something close to what I am looking for?
Logic:
Must be able to provide a starting year (ex. 2014).
Must start at the starting year and go 1 year past the current year (ex. If Start = 2014, it will show 2014, 2015, 2016.
The value of the option must be the last day of the month in date format (ex. <option value="01/31/2015">January 2015</option>
Output:
<select class="s2 span4" name="desiredCompletion" placeholder="Select a Desired Completion Date">
<optgroup label="2016">
<option value="01/31/2016">Janurary 2016</option>
<option value="02/28/2016">Feburary 2016</option>
<option value="03/31/2016">March 2016</option>
...
</optgroup>
<optgroup label="2015">
<option value="01/31/2015">Janurary 2015</option>
<option value="02/28/2015">Feburary 2015</option>
<option value="03/31/2015">March 2015</option>
...
</optgroup>
<optgroup label="2014">
<option value="01/31/2014">Janurary 2014</option>
<option value="02/28/2014">Feburary 2014</option>
<option value="03/31/2014">March 2014</option>
...
</optgroup>
</select>
This function should do:
function createYearSelect($from) {
// open select
echo "<select class=\"s2 span4\" name=\"desiredCompletion\" placeholder=\"Select a Desired Completion Date\">";
// for each year from the one specified to the one after current
foreach(range($from, date('Y') + 1) as $year) {
// open optgroup for year
echo "<optgroup label=\"$year\">";
// foreach month
foreach (range(1, 12) as $month) {
// timestamp of first day
$time = strtotime("$year-$month-01");
// create option with date() formatting
echo "<option value=\"".date("m/t/Y", $time)."\">".date("F Y", $time)."</option>";
}
// close optgroup for year
echo "</optgroup>";
}
// close select
echo "</select>";
}
Here's a bit more elegant way
foreach(range(2014, 2015) as $year){
foreach(range(1, 12) as $month){
echo date('Y.m.d', strtotime('Last day of ' . date('F', strtotime($year . '-' . $month . '-01')) . $year)) . PHP_EOL;
}
}
This outputs the list of the last days of months for the years in specified range.
2014.01.31
2014.02.28
2014.03.31
2014.04.30
...
Using the DateTime class you can create dates using natural language.
Eg,
$date = new DateTime("-1 Year");
echo $date->format('F Y'); // Prints 'December 2014'
You can use a decrementing for loop to create many dates going back in time.
Check for the function below which gives the total number of days in a month
date("t");
Further details are available at http://php.net/manual/en/function.date.php

PHP - Displaying multiple years from the current year

I was wondering if someone could help me.
I want to display a dropdown ( select ) box with 10 years starting with the current year.
I know displaying the year is done by using
date("Y");
But im not sure how to display the 10 years after that automatically.
Any help would be greatly appreciated.
Cheers
Try:
echo('<select name="year">');
for ( $i = date("Y"); $i < date("Y")+11; $i++ )
{
echo('<option value="'.$i.'">'.$i.'</option>');
}
echo('</select>');
See result here http://codepad.viper-7.com/8S0Ogi
PHP has a slew of Date/Time functions.
One you have instantiated a DateTime Object, you'll be able to manipulate any date or time using simple date_add or date_sub functions...
However, for your situation, it would be possible and a lot simpler to simply iterate over the number of years you want in the dropdown and simply increase the value for each option.
$yearSpan = 10;
$currentYear = date("Y");
$html = '<select id="foobar">';
for($i = $currentYear; $i<=$yearSpan; $i++) {
$html .= "<option value='".$i."'>".$i."</option>";
}
$html .= '</select>';
That should populate $html with content similar to this -
<select id="foobar">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
...
<option value="2021">2021</option>
<option value="2022">2022</option>
</select>
You can try with this:
<select>
<option>Select One</option>
<?php
$year = date("Y");
$yearto = date("Y")+10;
while ($year <= $yearto)
{
echo "<option value='".$year."'>".$year."</option>";
$year++;
}
?>
</select>

Populating a dropdown with recent years

I have a <select> dropdown with which I want to populate the previous 20 years as well as the current year. How would I go about this?
It would be simpler to use strtotime()
echo strtotime("+1 year");
You can also use this inside a loop
echo strtotime("+".$i." year");
Where $i is the index in the loop.
This should be all you need:
echo "<select>";
for ($i = date("Y") - 20; $i <= date("Y"); $i++)
{
echo "<option value='{$i}'>{$i}</option>";
}
echo "</select>";
What about trying something like this:
for (i=0;i<20;i++) {
echo mktime(0, 0, 0, date("m"), date("d"), date("Y")+i);
}
Seems like you just need a snippet like this:
$current_year = date("Y");
$min_year = 1990;
echo '<select name="year">';
echo '<option value="">Select Year..</option>";
for($i=$min_year; $i<=$current_year; $i++) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
echo '</select>';

Categories