diff($bday);" method."/> diff($bday);" method.">

Home  >  Article  >  Backend Development  >  How to output age based on birthday in php

How to output age based on birthday in php

藏色散人
藏色散人Original
2018-11-19 10:24:167934browse

php method to output age based on birthday: first create a PHP sample file; then output the age through the "$today->diff($bday);" method.

How to output age based on birthday in php

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, Dell G3 computer.

This article mainly introduces to you the implementation method of php calculation of age, that is, calculation of age.

For PHP beginners, the problem of calculating age based on birthday may be a bit difficult. But as long as you master the idea of ​​​​implementing it, it is also very simple. PHP age calculation is also a common test point in our PHP interview questions.

Below we will introduce the implementation method of PHP age calculation through specific code examples.

<?php
$bday = new DateTime(&#39;15.9.1993&#39;); // 你的出生日
$today = new Datetime(date(&#39;m.d.y&#39;));
$diff = $today->diff($bday);
printf(&#39; 你的年龄是 : %d 岁, %d 月, %d 天&#39;, $diff->y, $diff->m, $diff->d);
printf("\n");

The calculation results are as follows:

How to output age based on birthday in php

#Then actually calculating age based on birthday is to calculate the difference between your birthday date and the current date. The birth date set here is September 15, 1993, and the calculated age is 25 years, February 4 days.

The printf() function means outputting a formatted string, that is, specifying the string and how to format the variables in it.

Possible format values:

%% - Returns a percent sign %

%b - Binary number

%c - ASCII value corresponding Characters of

%d - Decimal number with sign (negative, 0, positive)

%e - Scientific notation using lowercase (e.g. 1.2e 2)

%E - Use uppercase scientific notation (e.g. 1.2E 2)

%u - Decimal number without sign (greater than or equal to 0)

%f - Float Points (local setting)

%F - floating point number (non-local setting)

%g - shorter %e and %f

%G - shorter % E and %f

%o - octal number

%s - string

%x - hexadecimal number (lowercase letters)

% X - Hexadecimal number (uppercase letters)

Additional format value. Must be placed between % and the letters

Recommended reference tutorial: "PHP Tutorial"

This article is about the specific implementation method of PHP outputting age based on birthday. Everyone also Can be copied directly to local testing. This article has certain reference value and I hope it will be helpful to friends in need!

The above is the detailed content of How to output age based on birthday in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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