Desafio 010: Máquina do Tempo - @CursoemVideo de PHP
Resolution of the Tenth Challenge
Introduction to the Challenge
- The video addresses the resolution of a programming challenge that may seem simple but contains interesting elements worth exploring.
- Viewers are encouraged to comment on their age if they were born in 2007, as an example of calculating age based on the current year, 2023.
Age Calculation Logic
- A rule is established that users cannot input a birth year greater than the current year (2023), ensuring logical consistency in age calculations.
- The instructor introduces himself and emphasizes that this challenge is part of a series designed to enhance PHP skills.
Implementation Steps
- The instructor captures the current date using PHP's
datefunction, specifically focusing on retrieving the full year with uppercase 'Y'.
- He explains how to calculate a person's age by subtracting their birth year from the current or specified year.
Example Calculations
- An example is provided where someone born in 1978 would be calculated to be 45 years old in 2023, highlighting potential discrepancies based on actual birthday dates.
Form Creation in PHP: Understanding Basics
Introduction to Form Logic
- The logic for creating forms is straightforward, especially when using online PHP. Local servers offer more flexibility for enhanced form designs.
- A simple HTML form has been created to input the year of birth and calculate age based on the current year (2023).
Form Structure and Code Overview
- The initial setup includes an
index.phpfile linked with a CSS stylesheet, focusing on standard HTML structure.
- A basic form was constructed with two number input fields and a submit button, utilizing PHP to handle data submission.
Importance of HTML Knowledge
- It’s recommended that those unfamiliar with basic HTML take a foundational course before proceeding, as understanding this code is essential for effective form creation.
- The next challenge will involve adding a special component (a range slider), which will be demonstrated alongside the audience.
PHP Script Integration
- The script begins by capturing the current year and user input from the form fields to process age calculations.
- Variables are set up to retrieve user inputs such as birth year and desired calculation year, ensuring defaults are established if no input is provided.
Handling User Input
- Default values are assigned in case users do not enter their birth year; it defaults to 2000 while allowing adjustments for years prior to 1900.
- The script uses consistent variable naming conventions between PHP variables and form element names for clarity.
Displaying Results Dynamically
- If no data is entered upon first loading, the current year will automatically populate in the relevant field.
- Dynamic display updates include showing pre-filled values in both birth year and target calculation year fields upon page load.
Finalizing Form Functionality
- Additional text clarifies what information users need to provide, enhancing usability by indicating that they can see their age calculated based on their inputs.
- Upon clicking "Calculate Age," results should reflect accurate calculations based on user-provided data.
Conclusion of Setup Process
Age Calculation in PHP
Setting Up the Age Calculation
- The speaker introduces a new variable for age calculation, referencing their birth year (1978) and the current year to determine age.
- Demonstrates how to calculate age by inputting different years, confirming that someone born in 1978 would be 45 years old in 2050.
Handling Edge Cases
- Discusses a potential issue with calculating age for years before birth; if someone was born in 1978, they would be -58 years old in 1920.
- Emphasizes the need for conditional statements (if statements) to handle cases where users input unrealistic birth years.
Implementing Input Validation
- Suggests creating conditions to prevent users from entering future birth years (e.g., 2050), as it is not possible given the current year is 2023.
- Proposes setting minimum and maximum values for input fields: minimum of 1900 and maximum of the current year (2023).
Refining User Input Restrictions
- Explains that only valid inputs should be accepted; if a user tries to enter a year greater than the current year, an error message will appear.
- Introduces logic to restrict entries so that only valid past years are accepted, ensuring accurate age calculations.
Future Learning and Support Resources
- Mentions additional support systems available through their course platform, encouraging viewers to become supporters for extra benefits like early access to lessons.
- Highlights advantages of becoming an annual supporter, including receiving multiple credits towards certificates each month.
Conclusion and Encouragement
- The speaker wraps up by acknowledging existing issues while assuring viewers that upcoming modules will address these challenges effectively.