Creating a form that allows user to select a year

I’m working on my second college assignment in PHP. Understanding the logic here - on a scale of 1 to 10 - I’m barely a 1. The college offers no tutoring for programming and my professor’s office hours total 6 hours a week and conflict with my schedule. I’ve advertised for a tutor without success. I’m on my own.

I have to follow very specific instructions. I’m needing to create a form that allows a user to select a year and then another option to choose a month. When the user clicks Submit, the page will refresh and display a calendar that represents the year and month the user selected. We never discussed dealing with dates in class. When he gave the assignment, he mentioned that we hadn’t gone over it but that our course book had a chapter covering dates and we should rely on it to complete the assignment. I’ve read the chapter 3 times and I’m beyond lost.

Right now I’m going to focus on creating a from that allows the user to select a year (current year-5 … to 2050). I know how to hard code the years into the HTML but we can’t do that. The options must be dynamically generated using PHP. I would greatly appreciate help figuring this out.

Please let me know how to add my current code attempt and I’ll edit my post to include it.

Your course book should have covered either or both the php date() function and the php datetime class. If you read the php.net documentation for either of those, you will find that they operate on the current date/time if no starting date/time is given.

Next, both of those methods have (the same) format specifiers. The ‘Y’ specifier corresponds to the year part of the date/time. So, you should at this point be able to piece together and test two different ways of getting the current year. Getting the current year - 5 is just a subtraction operation (if you wanted to be tricky, you can specify the current date/time - 5 years as the starting date/time for the date() function, via strtotime(), or by subtracting a 5 year interval for the datetime class.)

You would then just loop from the starting value to the ending value to produce the select/option choices. Of the different possible ways of looping, you may want to consider a foreach/range combination -

foreach(range($starting_year,2050) as $year)
{
    // use the $year variable as needed
}
1 Like
  1. I am available, usually, if you have specific questions as is the board. If there are issues with your account, I can also (probably) fix them.

If I am reading this correctly,

DEFINE('STOP_YEAR', 2050);
DEFINE('YEARS_TO_GO_BACK', 5);
$cur_date = new DateTime();
foreach(range($cur_date->modify('-' .YEARS_TO_GO_BACK . ' year')->format('Y'),STOP_YEAR) as $year)
{
	echo "{$year}\n";
}

Now, you need to convert that over to make it create an option list for a dropdown.

And for your research:
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/function.range.php

I might consider offering blocks of time as a tutor. My only concern is that the requirements imposed on you may not be to current coding standards or be the “correct” way to solve a problem. If your interested, PM me the exact details of what you need to do.

1 Like

I had the same thought!!!

1 Like

I’m embarrassed to admit that I can’t figure out how to PM you. That’s a first. As if this trouble with PHP wasn’t enough … geez.

This is the code I attempted after I posted my question last night. It works other than setting the default year to the current year. Hours spent on just this. From 7 pm last night until 4 am this morning. That is insane. And I can’t even fully read the code I’m using. It’s basically trial and error for me. I need to find a way to actually understand the logic here.

Enough complaining … How can I fix this to make the default year be 2019 rather than 2014, which is displaying now? Or is this bad code? A note from my professor this morning :

“Focus on how to create DateTime objects and the formats you can use with the DateTime class” … He offers a few pages in our book for reference (not helpful for me … still don’t understand) and then states, “The formats are the key to the assignment.”

Current code attempt:

<form id=“yearForm” name=“yearForm” method=“post” action="">
<label for=“select_year”>Select the year: </label>
<?php
// Sets the default year to be the current year.
$current_year = date(‘Y’);
// Year to start available options.
$earliest_year = ($current_year - 5);
// Set your latest year you want in the range.
$latest_year = 2050;

echo ‘<select>’;
// Loops over each int[year] from current year, back to the $earliest_year [1950]
foreach ( range( $earliest_year, $latest_year ) as $i ) {
// Echos the option with the next year in range.
echo ‘<option value="’.$i.’" ‘.($i === $current_year ? ’ selected=“selected”’ : ‘’).’>’.$i.’</option>’;
}
echo ‘</select>’;
?>
</form>

@phdr already showed/told you how to do this.

1 Like

Yes. I saw that after I had already worked on it through the night. I don’t have the knowledge yet to understand the instructions he has given for editing the code. I don’t understand looping. I don’t know how to take the example code he has given and complete the task. I had no knowledge of PHP until a month ago. I only hear lectures from a professor who has his own unique way of communicating. I’ve completed only one assignment before this one. I can copy and paste code easy enough but I’m trying to develop some actual logic behind what I’m doing. That’s very difficult when I struggle to even understand what someone is saying about it. I wish I could devote more time to just PHP but I have 2 other challenging courses plus my internship this semester. This class already takes more time each week than the other 3 courses combined.

For this reason, I thought it might be best to use the code that I wrote last night and edit it to correct the default year … rather than basically starting over. That may seem disrespectful since I came here asking for help and then seem to reject the help offered. That is certainly not what I’m trying or wanting to do. It’s extremely frustrating for me to ask for help and then be unable to understand the solution. And it’s equally uncomfortable for me to see a solution but need to ask the author to talk to me like I’m an 8 year old … because that’s exactly how I’m feeling. I have maintained a 4.0 in college. This means nothing other than proof of my efforts. I’ve never felt more stupid than I do right now. It sucks.

I appreciate the time you took to explain and offer code. I don’t want to ask you to elaborate … because you’ve done that. I just can’t figure out how to take the code you’ve given me and complete the task. I don’t yet have enough experience to read your instructions and, from that, use the code you’ve given me and put it all together. So aggravating

This may be what your professor is looking for. I would highly recommend you actually learn what each part is doing. You may be asked about it. Besides, just doing a copy/paste isn’t going to help you.

<?php declare (strict_types = 1);

$date = date_create(date('Y'));
date_sub($date, date_interval_create_from_date_string("5 year"));
$minus = date_format($date, "Y");

$date = date_create(date('Y'));  
date_add($date, date_interval_create_from_date_string("31 year"));
$plus = date_format($date, "Y");

?>
<form method="post">
    <select name="year">
        <?php foreach (range($minus, $plus) as $year): ?>
        <option value='<?=$year;?>'><?=$year;?></option>
        <?php endforeach;?>
    </select>
</form>

This is what I explained in a previous post. I’m not satisfied with simply copy/paste. I must develop the logic necessary to complete these assignments and that has been the greatest challenge. I’m not getting it from my class or professor. The other students are struggling and frustrated. I’ve reached out to local coding “meetups”, facebook groups, a local university and even approached the dean at the college I attend. Efforts to find someone to help me actually learn PHP have been futile. Like I mentioned in another post - If I could only devote all my time to PHP. Thank you for the code. I’ll try my best to make sense of it.

Here is a version that does the same thing but selects the current year.

Since you mention “other students struggling” perhaps we can work out a live group tutoring session. It would be much more cost effective to do personal tutoring as a group. Only thing is, the whole class will end up submitting the same code.

<?php declare (strict_types = 1);

$today = date('Y');
$date = date_create($today);
date_sub($date, date_interval_create_from_date_string("5 year"));
$minus = date_format($date, "Y");

$date = date_create(date('Y'));
date_add($date, date_interval_create_from_date_string("31 year"));
$plus = date_format($date, "Y");
?>
<form method="post">
    <select name="year">
    <?php
        foreach (range($minus, $plus) as $year){
        $selected = ($today == $year) ? 'selected' : '';
        echo "<option value='$year' $selected> $year</option>";
        }
        ?>
    </select>
</form>
1 Like

I love the idea of group tutoring but I can see our professor failing all of us for submitting the same code. He has encouraged us to work together but warned against 2 students submitting identical code. This is the professor that gave me an F (my first and only failing grade) on my first JavaScript assignment because I didn’t use his methods for all of the assignment … though he had stated that we could use different code as long as we could explain it. My assignment was perfect and performed as expected but he said he failed me for using code different from what he had instructed during his lectures. There were about 15 students when the class started. Down to 8 now and that will likely decrease next week. Anyone with less 50% in the class will be dropped and I know several who failed the first assignment and likely to fail this one. We can’t even work together … That would be like the blind leading the blind. There is one young man who is already employed as a coder and he has a solid grasp of PHP. He’s been approached by 2 students (those I’m aware of) and he has no interest in tutoring. He doesn’t enjoy it and just wants to get through the class and move on. I can relate.

Here is the OOP Way to do the same thing. Cant say I agree at all with the methods of teaching I hear about on the forums. The point is to learn. Cant learn if people are being dropped.

<?php declare(strict_types=1);

$today = date('Y');

$date = new DateTime($today );
$date->sub(new DateInterval('P5Y'));
$minus = $date->format('Y');

$date = new DateTime($today );
$date->add(new DateInterval('P31Y'));
$plus = $date->format('Y');
?>
<form method="post">
    <select name="year">
    <?php
        foreach (range($minus, $plus) as $year){
        $selected = ($today == $year) ? 'selected' : '';
        echo "<option value='$year' $selected> $year</option>";
        }
        ?>
    </select>
</form>

I don’t understand the difference between the code versions you’ve written. I’m trying to though. I had to google what OOP means. And I researched “strict-types”. It’s never been introduced in class so it won’t be allowed on this assignment. THIS is why getting through this class is so damn hard. I can get help but the solution isn’t accepted by the professor. And when I email him for help, I get a response I don’t understand or one that is so vague that it’s pointless. I had this professor for Client-Side programming last semester. We ended the semester with only 3 students. I made it through with the help of a prior student of his who understood the professor’s expectations. … except for that first assignment that I failed, of course.

I would love to see an application this “Professor” has written, that is if he even has. Perhaps you should cancel his class and sign up for the “Benanamen Skool O’ Code”. I will only charge you 75% of what University is billing you. I guarantee you will come out much smarter and knowledgeable.

1 Like

I wish I could and I appreciate your offer. My decision to attend college again (after 25 years) was made 2 years ago when my ruptured discs left me bedridden for a month. I could no longer physically manage my job (oil field sales rep). I was offered a college grant through the state rehabilitation department and additional grant money through a single parent scholarship. I feel obligated to complete the degree now (I graduate in May). I started this with the goal of gaining skills necessary for front-end web development. I chose the college based on information given to me by their academic adviser. I was encouraged to follow a degree plan for computer programming with a focus on web development (one of the four tracks I could choose). I was told this would provide me with the knowledge needed to find success upon graduation. So far, I’ve built one website and that was a year ago. I love HTML and CSS but I rarely get to use it. The dean insisted that I take GUI and JAVA classes even after I voiced my concern to her that my time was being wasted on courses that would not help me in web development. The professor teaching both of those classes confirmed that they were a waste of my time. Eventually, the dean approved of substitutions. They are letting Server-Side Programming (PHP) and Securing E-Commerce as credits towards my degree. These were the closest thing to web development that the school offers. Aside from a Web Development course that I’ve already taken and that consisted solely of following a simple, written guideline to edit already built web pages. No challenge and I couldn’t build a website from scratch today if I had to. I could have learned a great deal more and focused on what I really needed to … on my own. If only I knew then what I know now. College has been a waste of time for me.

Then he is a junior and won’t do well as a senior. Most of my time is spent mentoring junior developers on their practices and career paths.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service