2
1 <?php 2 // Part 1: Use date("xxx") with only one parameter to output TODAY's info 3 // such as current day, month and year. 4 5 $today_day = date('d'); 6 $today_month = date('m'); 7 $today_year = date('Y'); 8 9 echo "Today's date is " . $today_day . '/' . $today_month . '/' . $today_year . '<br>'; 10 // Today's date is 03/02/2014 11 12 // Part 2: My date of birth. 13 $dob_day = '31'; 14 $dob_month = '01'; 15 $dob_year = '1968'; 16 17 // Output method 1: using . and strings: 18 echo 'My date of birth is ' . $dob_day . '/' . $dob_month . '/' . $dob_year . '<br>'; 19 // My date of birth is 31/01/1968 20 21 // Output method 2: Using mktime( ) to create the timestamp: 22 $dob = mktime(0, 0, 0, $dob_month, $dob_day, $dob_year); 23 echo 'My date of birth is ' . date('d/m/Y', $dob) . '<br>'; 24 // My date of birth is 31/01/1968 25 26 // Part 3: Calculate age. 27 if ($dob_month > $today_month) // Not yet reach birthday month this year (need to minus 1) 28 { 29 $age = $today_year - $dob_year -1; 30 echo 'Path 1 <br>'; // Debugging message 31 32 } Macintosh HD:Applications:XAMPP:xamppfiles:htdocs:php:dob.php: 1/2

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

Embed Size (px)

DESCRIPTION

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

Citation preview

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

1 <?php 2 // Part 1: Use date("xxx") with only one parameter to output TODAY's info 3 // such as current day, month and year. 4 5 $today_day = date('d'); 6 $today_month = date('m'); 7 $today_year = date('Y'); 8 9 echo "Today's date is " . $today_day . '/' . $today_month . '/' . $today_year . '<br>';10 // Today's date is 03/02/201411 12 // Part 2: My date of birth.13 $dob_day = '31';14 $dob_month = '01';15 $dob_year = '1968';16 17 // Output method 1: using . and strings:18 echo 'My date of birth is ' . $dob_day . '/' . $dob_month . '/' . $dob_year . '<br>';19 // My date of birth is 31/01/196820 21 // Output method 2: Using mktime( ) to create the timestamp:22 $dob = mktime(0, 0, 0, $dob_month, $dob_day, $dob_year);23 echo 'My date of birth is ' . date('d/m/Y', $dob) . '<br>';24 // My date of birth is 31/01/196825 26 // Part 3: Calculate age.27 if ($dob_month > $today_month) // Not yet reach birthday month this year (need to minus 1)28 {29 $age = $today_year - $dob_year -1;30 echo 'Path 1 <br>'; // Debugging message31 32 }

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

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

33 elseif ($dob_month == $today_month) // Just reach birthday month this year.34 {35 if ($dob_day > $today_day) // Not yet reach birthday day this year (need to minus 1)36 {37 $age = $today_year - $dob_year -1;38 echo 'Path 2 <br>'; // Debugging message39 }40 else // Already reach birthday day this year.41 {42 $age = $today_year - $dob_year;43 echo 'Path 3 <br>'; // Debugging message44 } // End of else for day.45 } 46 else // Already after birthday month this year.47 {48 $age = $today_year - $dob_year;49 echo 'Path 4 <br>'; // Debugging message50 }51 52 echo 'I am now ' . $age . ' years old. <br>';53 // Path 4 54 // I am now 46 years old. 55 ?>

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