Home Backend Development PHP Tutorial PHP basic case 2: Calculating student age

PHP basic case 2: Calculating student age

Nov 02, 2020 pm 10:42 PM
php

1. Requirements Analysis

In order to conveniently, accurately and quickly display the student's age, the system usually automatically calculates it based on the student's date of birth. Next, please use PHP variables to save the student's year, month, and day respectively, and obtain the current year, month, and day through the data function in PHP, and finally calculate the student's age.

For example:

The date of birth is: August 2, 2000

If it is May 2020, the age is 20 years old

If it is October 2020, the age is 19 years old

2. Design ideas

  1. How to define Variables store student information. What are these variables?

  2. #How to define variables to save the year, month, and day of a student’s birth?

  3. #How to get the year, month and date of the current time?

  4. #How to calculate a student’s age from birth to the current year?

  5. #How to determine whether a student has a birthday?

3. Knowledge reserve

1. Introduction to PHP Date/Time

Date/Time function allows you to Get the date and time from the server where the PHP script is running. You can use the Date/Time functions to format dates and times in different ways.

2. Definition and usage

date() function formats the local date and time and returns the formatted date string.

3. Syntax

date(format,timestamp);
Returns a string generated by integer timestamp according to the given format string. If no timestamp is given, the local current time is used. In other words, timestamp is optional and the default value is time().

For details, please read this article: https://www.jb51.net/article/148360.htm

4. Several commonly used parameters

PHP basic case 2: Calculating student age

4. Code implementation

<?php
//定义变量保存学生出生的年、月、日
$stu_by = 2001;
$stu_bm = 8;
$stu_bd = 07;
//获取当前时间的年份、月份和日期
$cur_y = date(&#39;Y&#39;); //4位数字完整表示的年份
$cur_m = date(&#39;n&#39;); //数字表示的月份,没有前导零,1~12
$cur_d = date(&#39;j&#39;); //月份中的第几天,没有前导零,1~31
//计算学生从出生到当前年的周岁
$age = $cur_y - $stu_by;
//判断学生是否已过生日
if($cur_m < $stu_bm || $cur_m==$stu_bm && $cur_d<$stu_bd){
$age--;
}
?>
Copy after login

5. Effect display

Today is November 2, 2020. PHP basic case 2: Calculating student age

PHP basic case 2: Calculating student age

The above is the detailed content of PHP basic case 2: Calculating student age. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles