Desafio 010: Máquina do Tempo - @CursoemVideo  de PHP

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 date function, 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.php file 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.
Video description

Você sabe como calcular a idade de uma pessoa pelo PHP? Sabe capturar o ano atual do servidor usando PHP? Veja esse vídeo até o fim para responder todas essas perguntas. Esse vídeo faz parte do seu curso de PHP Moderno 100% grátis, criado pelo professor Gustavo Guanabara, do canal @cursoemvideo. Quer acompanhar o primeiro módulo, com os primeiros passos em PHP? Acesse diretamente a nossa playlist: https://www.youtube.com/playlist?list=PLHz_AreHm4dlFPrCXCmd5g92860x_Pbr_ O que vamos aprender nesse vídeo? 00:00 - Décimo desafio em PHP 02:31 - Olha só quantos vencedores 03:42 - Capturando o ano atual com PHP 05:01 - Calculando a idade de uma pessoa 06:14 - Calculando idade em um ano específico 06:54 - Criando o desafio com formulário 09:18 - Capturando dados do formulário 11:29 - Testando e melhorando o exercício 12:51 - Calculando a idade via formulário 14:11 - Resolvendo pequenos problemas com datas 16:36 - Seja apoiador do nosso projeto 17:16 - O que vem a seguir? Gostou da aula? Então torne-se um Gafanhoto APOIADOR do CursoemVídeo acessando o site cursoemvideo.com/apoie Inscreva-se no nosso canal de cortes! https://www.youtube.com/channel/UCNp7wEXH_E2E5ieJRv8RCFA?sub_confirmation=1 Seja membro deste canal no YouTube e ganhe benefícios: https://www.youtube.com/channel/UCrWvhVmt0Qac3HgsjQK62FQ/join Curso em Vídeo Seja um apoiador: https://cursoemvideo.com/apoie Doe Bitcoins: 18A9Se4FK2hSsDT7xZ7n5FdRQ47WCjcftw Site: https://www.cursoemvideo.com YouTube: https://www.youtube.com/cursoemvideo Canal de Cortes: https://www.youtube.com/channel/UCNp7wEXH_E2E5ieJRv8RCFA Instagram: https://www.instagram.com/cursoemvideo Facebook: https://www.facebook.com/cursosemvideo Patrocínio HOSTNET: https://www.hostnet.com.br/hospedagem-de-sites/ EXPLICADORES.NET: https://www.explicadores.net.br/cursoemvideo/ ESTUDONAUTA: https://www.estudonauta.com/matricula/ #cursoPHP #modulo01 #desafio010