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.
Related
Two days ago I had an interview as a php developer and they gave me a task to perform.
I completed 90% of the task, but I failed while trying to add date, month and year together.
I have 3 dropdowns for date, month and year:
Date Of Birth : <br />
Date : <select name="date">
<?php $i=1; for($i=1; $i<=31; $i++){ ?> <option> <?php echo $i?> </option> <?php } ?>
</select>
Month : <select name="month">
<option value="January">January</option>
<option>Febuary</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
</select>
Year :
<?php
$currently_selected = date('Y');
$earliest_year = 1950;
$latest_year = date('Y');
print '<select name="year">';
foreach ( range( $latest_year, $earliest_year ) as $i ) {
print '<option value="'.$i.'"'.($i === $currently_selected ? ' selected="selected"' : '').'>'.$i.'</option>';
}
print '</select>';
?>
I have only one column in the database for the date of birth.
I want to add the date, month and year together, which should result in the following: date/month/year.
How could I accomplish this?
if(isset($_POST['submit'])){
$name = trim($_POST['username']);
$date = $_POST['date'];
$month = $_POST['month'];
$year = $_POST['year'];
if($name == ''){
$error = "Add Name";
}
$DateOfBirth = $date.'/'$month;
if(!$error){
echo $DateOfBirth;
}
}
Assuming your date of birth field is a MySQL DATE field, you need to make sure that you are creating a date in the correct format i.e. YYYY-MM-DD. You can do that by using strtotime to convert your input data into a timestamp, and then date to convert that into the appropriate format:
$DateOfBirth = date('Y-m-d', strtotime("$date $month $year"));
For example:
$year = 2015;
$month = 'August';
$date = 5;
$DateOfBirth = date('Y-m-d', strtotime("$date $month $year"));
echo $DateOfBirth;
Output:
2015-08-05
Demo on 3v4l.org
Note
If you really want the result to be in dd/mm/yyyy format, just change the first input to date to 'd/m/Y'.
The reason is your are not concatenating the variables in php. Check below code. You are almost there but with small coding mistake.
What you are trying to do is concatenate a variable with a string. So you have to concatenate variable with a '.' and then add your string. In here / works as the string. When adding a string it should be with in quotations '/' . And the final output will be something like this. $variable . '/'. Now concatenate another variable. Then it would be like this. $variable01 . '/' . $variable02.
$DateOfBirth = $date.'/'.$month.'/'.$date ;
Output
12/02/2020
Check the added example with respect to your requirement
example
I have a dropdown list Year made in php with years from current year until current year+10:
<p>Year:
<select name="sel_year">
<?php
$year=date("Y")-1;
for ($i = 0; $i <= 10; $i++)
{
$year = $year+1;
echo("<option value=\"$year\">$year</option>");
}
?>
</select>
And I want to make a second dropdown list with months that are based on the selection made on the previous list (Year), the thing is that I can only use html and php and I need to do this on the same page and, if possible, without using a submit button.
This is what I have so far:
<p>Month:
<select name="sel_month">
<?php
if(sel_year==(date("Y")))
{
for ($month = date("m"); $month <= 12; $month++)
{
$monthName = date('F', mktime(0, 0, 0, $month, 10));
echo("<option value=\"$month\">$monthName</option>");
}
}
?>
</select>
In this part I only want to allow the user to select the months from now until December (inclusive) if the selected year previously is the same year as we're in. Any ideas?
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>
Is there a simple if statement to default select the correct date from the dropdown? so for example if the date is 02/11/16 then it will default select
01/28/16
Basically what I'm asking for is an if statement that default selects the drop-down that the date is within
<?php
date_default_timezone_set('US/Eastern');
$currenttime = date('d:m:y:h:i:s A');
list($day,$month,$year) = split(':',$currenttime);
$currentdate = "$month/$day/$year";
?>
<form>
<select>
<option>01/14/16</option>
<option>01/28/16</option>
<option>02/14/16</option>
///the list goes on for ages so i saved time and cropped out the rest of the dates.
</select>
</form>
Put all the dates in an array, and loop through them. Then you can test them and decide whether the current date is in the billing period.
$dates = array('16-01-14',
'16-01-28',
'16-02-14',
...);
$currentdate = date('y-m-d');
?>
<form>
<select>
<?php
foreach ($date as $i => $d) {
if ($currentdate >= $d && ($i == count($dates)-1 || $currentdate < $dates[$i+1])) {
$selected = "selected";
} else {
$selected = "";
}
list($year, $month, $day) = explode('-', $d);
echo "<option $selected>$month/$day/$year</option>";
}
?>
</select>
</form>
I changed the format of $currentdate to y-m-d so that they can be compared as strings to see if the date is in a range.
As it loops through the list of dates, it tests whether the current date is between that date and the next date in the array (or it's the last date in the array). If it is, it adds the selected attribute to the <option>.
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