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

0 %
100 %
Information about PHP built-in functions date( ) and mktime( ) to calculate age from date...
Education

Published on February 5, 2014

Author: puahhlss

Source: slideshare.net

Description

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

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

Add a comment

Related presentations

Related pages

PHP: mktime - Manual - PHP: Hypertext Preprocessor

Function to generate array of dates between two dates (date range array) Read more

PHP: date - Manual - PHP: Hypertext Preprocessor

... flexibility of PHP's built-in functions ... than the start date, subtract a week. // Calculate net ... a certain date. Rather than using mktime() ...
Read more

PHP calculate age - Stack Overflow

PHP calculate age. up vote 84 down vote ... function get_age($date, ... Since birthays or mostly provided by full dates on birth location and they are ...
Read more

PHP: mktime - Manual - PHP: Hypertext Preprocessor

mktime (PHP 4, PHP 5, PHP 7) mktime ... This number is used to compute the age. Read more

How to calculate age from Date of Birth? | Drupal.org

... from this I need to auto calculate age, ... How to calculate age from Date of Birth? ... $age = (date("md", date("U", mktime(0, 0, 0 ...
Read more

PHP 5 Date and Time - W3Schools Online Web Tutorials

The PHP Date() Function. The PHP date() function formats a timestamp to a more readable date and time. Syntax. ... Create a Date With PHP mktime()
Read more

windows - PHP date calculation - Stack Overflow

... base_year = min(date("Y", $d1), date("Y", $d2)); $diff = mktime(0, 0 ... is no built-in function in PHP for ... examples/Calculate-Age-With-PHP ...
Read more

Date Calculator: Add to or Subtract From a Date

The Date Calculator calculates the duration between 2 dates. ... Alternative Age Calculator; Date Pattern Calculator; ... Date Calculator: ...
Read more

Date Functions in SQL Server and MySQL - W3Schools Online ...

Returns the number of days between two dates: DATE ... The following table lists the most important built-in date functions in ... PHP, jQuery, and XML ...
Read more

PHP :: Bug #17123 :: mktime(), localtime(), strtotime ...

mktime(), localtime(), strtotime(), etc. are broken ... The problem is that all of the PHP date functions are ... there's no way to calculate the age ...
Read more