This article mainly introduces to you

Age = age

That is

Calculate how old

The implementation method of.


For PHP beginners, figuring out the age problem based on birthdays can be a bit difficult. But as long as master its implementation ideas, is also very simple. PHP calculation of age is also a common question in our PHP interview questions.

Here we will introduce you through specific code examples
Age = ageThe implementation method of.

1

2

3

4

5

6

$bday = new DateTime (‘ 15.9.1993 ‘); // Your birthday

$today = new Datetime(date(‘m.d.y’));

$diff = $today->diff($bday);

Printf (” your age is: % d, % d, % d days’, $diff – > y, $diff – > m, $diff – > d);

printf(“\n”);




So to calculate your age by birthday, you calculate the difference between your birthday and your current date. The date of birth is set as September 15, 1993, and the calculated age is 25 years, 2 months and 4 days.

Where the function printf()Represents a formatted string for output, that is, specifying the string and how to format its variables.

Possible format values:

%% – Returns a percent sign %

%b – Binary number

%c – The character corresponding to the ASCII value

%d – Decimal number with plus or minus signs (negative, 0, positive)

%e – use lowercase scientific notation (for example, 1.2e+2)

%E – scientific notation in uppercase (for example, 1.2E+2)

%u – Decimal number without signs (greater than or equal to 0)

% F – floating point number (local setting)

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

%g – the shorter %e and %f

%G – the shorter %E and %f

%o – Octal number

%s – A character string

%x – Hexadecimal number (lowercase letters)

%X – Hexadecimal number (uppercase)

Additional format values. Must be placed in % and letter

This article is about PHP output age according to the birthday of the specific implementation of the introduction, you can also copy directly to the local test. This article has certain reference value, hope to be helpful to the friend in need!

So that’s how PHP prints the age by date of birth,