Populating a dropdown with recent years - php

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>';

Related

How to loop through time in select box PHP

I have a select drop down where I want to list times in the format of HH:MM with 1 minute intervals. so the list will start at 00:00 and finish at 23:59
I understand how to create a loop in a select drop down that will output 0-10
<select><?php for($i=0; $i<10; $i++){echo "<option>" . $i . "</option>";} ?>
</select>
and I understand how to output the time as HH:MM
<option><?php echo date('h:i', $supportrequest->startTime); ?</option>
But I can't work out how to do a combination of the two as I'm not sure what the parameters of the for loop should be
Using DatePeriod it'd be like that:
<?php
$begin = (new DateTime())->setTime(0,0,0); // create start point
$end = (new DateTime())->setTime(23,59,59); // create end point
$interval = new DateInterval('PT1M'); // set the interval to 1 minute
$daterange = new DatePeriod($begin, $interval ,$end); // create the DatePeriod
echo "<select>";
foreach($daterange as $date){ // loop through that period
echo "<option value='".$date->format("H:i") . "'>".$date->format("H:i")."</option>\n";
}
echo "</select>";
Using these classes makes it now easy to modify if you f.e. only want to have every 30 minutes, or need a different output format.
Do you really want to have a drop-down with one thousand, four hundred and forty option values (24 * 60 = 1440)? I think it would be better to have two <select> elements. You could style them to sit next to each other with a : in the middle if you wanted to keep the 'H:m' look.
<select id="hours">
<?php
for ($h = 0; $h < 24; $h++) printf("<option value=\"$h\"" . (!$h ? " selected" : "") . ">%02d</option>", $h);
?>
</select>
<select id="minutes">
<?php
for ($m = 0; $m < 60; $m++) printf("<option value=\"$m\"" . (!$m ? " selected" : "") . ">%02d</option>", $m);
?>
</select>
Converted Mukyuu's comment into an answer:
<select>
<?php
for($h=0; $h<24; $h++){
for($i=0; $i<60; $i++){
$time = date('h:i',strtotime($h.':'.$i));
echo "<option>".$time."</option>";
}
}
?>
</select>

Use static keyword in a for loop?

I want to use the static keyword in my for loop.
Is it possible? if yes then how?
Here is my code:
$current_time = date('h:i A');
<select>
for($i = 0; $i <= 5; $i++) {
if ($i=0) {
echo "<option>" . date("h:i A", $current_time) . "</option>";
}else{
static $tNow = strtotime("+15 minutes",strtotime($current_time));
echo "<option>" . date("h:i A", $tNow) . "</option>";
}
}
<select>
When I'm using the static keyword, I'm getting a php error.
I want to display a <select> element with each option being 15 minutes steps, like 12:00, 12:15, 12:30.
I don't know why you want to use static keyword in your code, but as suggested you should read here: http://php.net/manual/en/language.oop5.static.php, how and when to use static keyword.
If your goal is to display dropdown and getting option value different 15mint time
like this 12:00, 12:15, 12:30..... this is my solution:
$current_time[0] = date('h:i A');
echo"<select>";
echo "<option>" . $current_time[0] . "</option>";
$step=0;
for($i = 1; $i <=5; $i++) {
$step=$step+15;
$current_time[$i] = strtotime("+".$step." minutes", strtotime($current_time[0]));
echo "<option>" . date("h:i A", $current_time[$i]) . "</option>";
}
echo"</select>";
var_dump($current_time);
Working code here: http://ideone.com/zM0eue
Explanation: I used an array to contain all strtotime output in for loop. Each time I used the same $current_time[0] to calculate new $current_time[$i] with a $step variable incremented by +15 each time.

How can I echo out the selected date

does anyone know how I would echo out the selected date as text with the date month and year separated outside of the form? I tried echoing out $date $month and $year outside of the form however this doesn't give me the correct date thankyou for the help
<?
$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28',
'16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28',
'16-11-14','16-11-28','16-12-14','16-12-28');
$currentdate = date('y-m-d');
echo $currentdate;
?>
<form>
<select style="width:200px;">
<?php
foreach ($date as $i => $d) {
if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
$selected = "selected";
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
echo 'the current billing period is';
}
?>
</select>
</form>
Inside of your loop add a $selected_int variable like so:
foreach ($date as $i => $d) {
if ($currentdate >= $d && ($i == count($date)-1 || $currentdate < $date[$i+1])) {
$selected = "selected";
$selected_int = $i;
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
echo 'the current billing period is';
}
Then, you can reference it like:
echo date('Y-m-d', strtotime($date[$selected_int]));
Addition
I know you've already accepted the answer, but I also wanted to make a suggestion now that I see what you are using the $date for. Since you know the start date, and it is in 14-day periods, it would be easy to write that as part of the loop.
$start_date = date('Y-m-d', strtotime(date('Y').'-01-01'); //First day of the year, for the sake of argument.
$interval = 14;
for ($i = 0; date('Y') == date('Y', strtotime($start_date.' +'.($i * $interval).' days')); $i++) {//While this year is equal to the start date's year with the added interval [If I knew what your logic here was I could offer a better suggestion]
if ($currentdate >= date("Y-m-d", strtotime($start_date.' +'.($i * $interval).' days')) && (date('Y') < date("Y", strtotime($start_date.' +'.(($i + 1) * $interval).' days')) || $currentdate < date("m/d/Y", strtotime($start_date.' +'.(($i + 1) * $interval).' days')))) {
$selected = "selected";
$selected_int = $i;
} else {
$selected = "";
}
echo "<option $selected>" . date("m/d/Y", strtotime($start_date.' +'.($i * $interval).' days')) . "</option>";
}
Basically, this takes the start date, shows it as the first date option, then adds 14 days to it with each pass through. Your if/else statement should still be the same. It checks to see if you are on the last interval of the year, or if the current date is less than the next interval, and also that the current date is greater than the current interval.
After your loop, you can get the date by:
echo date("m/d/Y", strtotime($start_date.' +'.($selected_int * $interval).' days'));
I know it seems like a lot, but it would save you from having to make a date array to begin with.
Use strtotime instead list.
....
// list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
....
EDIT: Additional information - your code requires a lot modification and likely some structure changes but assuming this is for testing a method and "how to do" instead a final product.
You need to submit the selected date, catch it in the script and use the selected date to do what you need - i.e. retrieve data from database - and this should give you some idea.
<?php
// You need to create these dates by using another method. You cannot hard code these. You can create it with date functions easily.
$date = array('16-01-14','16-01-28','16-02-14','16-02-28','16-03-14','16-03-28','16-04-14','16-04-28','16-05-14','16-05-28','16-06-14','16-06-28','16-07-14','16-07-28','16-08-14','16-08-28','16-09-14','16-09-28','16-10-14','16-10-28','16-11-14','16-11-28','16-12-14','16-12-28');
// Checking if we have a posted form, with the button name user clicked
if (isset($_POST["btnSubmit"])) {
// This is your selected day - use it where you need:
$selectedDate = $_POST["selectedDate"];
// This is where your model start singing and gets necessary info for this date - just printing here as sample
print $selectedDate;
// I need dropDownDate to compare in the SELECT to preselect the appropriate date
$dropDownDate = strtotime($selectedDate);
} else {
// First time visit, preselect the nearest date by using current date
$dropDownDate = time();
}
?>
<form method="post">
<select name="selectedDate" style="width:200px;">
<?php
foreach ($date as $i => $d) {
if ($dropDownDate >= strtotime($d) &&
(!isset($date[$i+1]) || ($dropDownDate < strtotime($date[$i+1])))
) {
$selected = 'selected="selected"';
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>" . date("m/d/Y", strtotime($d)) . "</option>";
}
?>
</select>
<input type="submit" name="btnSubmit" value="Submit">
</form>
Note that I added a "submit" type input (to submit the form) and changed form method to "post", finally named SELECT as "selectedDate". I also changed your date comparison code line in the loop.
Hope this helps.

PHP Appending year the select menu

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

Displaying the list of months using mktime for the year 2012

Am am current facing a problem that need a solution ASAP.
I am trying to list all months of the current year(2012) by using the following code:
for ($m=1; $m<=12; $m++) {
$month = date('F', mktime(0,0,0,$m));
echo $month. '<br>';
}
But am getting the following unexpected output:
January
March
March
May
May
July
July
August
October
October
December
December
What am I doing wrong please help!!!
Try this:
for ($m=1; $m<=12; $m++) {
$month = date('F', mktime(0,0,0,$m, 1, date('Y')));
echo $month. '<br>';
}
not as with mktime but equally powerful
$array = array("January", "February",.....);
for ($m=0; $m<12; $m++) {
echo $array[$m]. '<br>';
}
I guess you should loop it in this manner.
for($i = 1 ; $i <= 12; $i++)
{
echo date("F",strtotime(date("Y")."-".$i."-01"));
echo "<br/>";
}
Or in your case, you want to use mktime()
for($i = 1 ; $i <= 12; $i++)
{
echo date("F",mktime(0,0,0,$i,1,date("Y")));
echo "<br/>";
}
Set day in mktime() to 1, otherwise conversion is performed: 30.2.2012 = 1.3.2012
$month = date('F', mktime(0,0,0,$m,1));
Pay attention to the localization.
You can also use this
setlocale(LC_TIME, 'it_IT');
for($m=1;$m<=12;$m++){
echo strftime("%B", mktime(0, 0, 0, $m, 12));
}
Changing the parameter on the function setlocale() you can display the localized text.
list of setlocale codes
If you use laravel
#for ($x = 1; $x <= 12; $x++)
<option value="{{$x}}">{{date('F', mktime(0, 0, 0, $x, 10))}}</option>
#endfor

Categories