1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

<?php
// Part 1: Use date("xxx") with only one parameter to output TODAY's info
// such as current day, month and year.
$today_day = date('d');
$today_month = date('m');
$today_year = date('Y');
echo "Today's date is " . $today_day . '/' . $today_month . '/' . $today_year . '<br>';
// Today's date is 03/02/2014
// Part 2: My date of birth.
$dob_day = '31';
$dob_month = '01';
$dob_year = '1968';
// Output method 1: using . and strings:
echo 'My date of birth is ' . $dob_day . '/' . $dob_month . '/' . $dob_year . '<br>';
// My date of birth is 31/01/1968
// Output method 2: Using mktime( ) to create the timestamp:
$dob = mktime(0, 0, 0, $dob_month, $dob_day, $dob_year);
echo 'My date of birth is ' . date('d/m/Y', $dob) . '<br>';
// My date of birth is 31/01/1968
// Part 3: Calculate age.
if ($dob_month > $today_month) // Not yet reach birthday month this year (need to minus 1)
{
$age = $today_year - $dob_year -1;
echo 'Path 1 <br>'; // Debugging message
}

Macintosh HD:Applications:XAMPP:xamppfiles:htdocs:php:dob.php: 1/2
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

elseif ($dob_month == $today_month) // Just reach birthday month this year.
{
if ($dob_day > $today_day) // Not yet reach birthday day this year (need to minus 1)
{
$age = $today_year - $dob_year -1;
echo 'Path 2 <br>'; // Debugging message
}
else // Already reach birthday day this year.
{
$age = $today_year - $dob_year;
echo 'Path 3 <br>'; // Debugging message
} // End of else for day.
}
else // Already after birthday month this year.
{
$age = $today_year - $dob_year;
echo 'Path 4 <br>'; // Debugging message
}
echo 'I am now ' . $age . ' years old. <br>';
// Path 4
// I am now 46 years old.
?>

Macintosh HD:Applications:XAMPP:xamppfiles:htdocs:php:dob.php: 2/2

PHP built-in functions date( ) and mktime( ) to calculate age from date of birth

  • 1.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 <?php // Part 1:Use date("xxx") with only one parameter to output TODAY's info // such as current day, month and year. $today_day = date('d'); $today_month = date('m'); $today_year = date('Y'); echo "Today's date is " . $today_day . '/' . $today_month . '/' . $today_year . '<br>'; // Today's date is 03/02/2014 // Part 2: My date of birth. $dob_day = '31'; $dob_month = '01'; $dob_year = '1968'; // Output method 1: using . and strings: echo 'My date of birth is ' . $dob_day . '/' . $dob_month . '/' . $dob_year . '<br>'; // My date of birth is 31/01/1968 // Output method 2: Using mktime( ) to create the timestamp: $dob = mktime(0, 0, 0, $dob_month, $dob_day, $dob_year); echo 'My date of birth is ' . date('d/m/Y', $dob) . '<br>'; // My date of birth is 31/01/1968 // Part 3: Calculate age. if ($dob_month > $today_month) // Not yet reach birthday month this year (need to minus 1) { $age = $today_year - $dob_year -1; echo 'Path 1 <br>'; // Debugging message } Macintosh HD:Applications:XAMPP:xamppfiles:htdocs:php:dob.php: 1/2
  • 2.
    33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 elseif ($dob_month ==$today_month) // Just reach birthday month this year. { if ($dob_day > $today_day) // Not yet reach birthday day this year (need to minus 1) { $age = $today_year - $dob_year -1; echo 'Path 2 <br>'; // Debugging message } else // Already reach birthday day this year. { $age = $today_year - $dob_year; echo 'Path 3 <br>'; // Debugging message } // End of else for day. } else // Already after birthday month this year. { $age = $today_year - $dob_year; echo 'Path 4 <br>'; // Debugging message } echo 'I am now ' . $age . ' years old. <br>'; // Path 4 // I am now 46 years old. ?> Macintosh HD:Applications:XAMPP:xamppfiles:htdocs:php:dob.php: 2/2