How to print dob in php?

Figured I'd throw this on here since this seems to be most popular form of this question.

I ran a 100 year comparison on 3 of the most popular types of age funcs i could find for PHP and posted my results (as well as the functions) to my blog.

As you can see there, all 3 funcs preform well with just a slight difference on the 2nd function. My suggestion based on my results is to use the 3rd function unless you want to do something specific on a person's birthday, in which case the 1st function provides a simple way to do exactly that.

Found small issue with test, and another issue with 2nd method! Update coming to blog soon! For now, I'd take note, 2nd method is still most popular one I find online, and yet still the one I'm finding the most inaccuracies with!

My suggestions after my 100 year review:

If you want something more elongated so that you can include occasions like birthdays and such:

function getAge($date) { // Y-m-d format
    $now = explode("-", date('Y-m-d'));
    $dob = explode("-", $date);
    $dif = $now[0] - $dob[0];
    if ($dob[1] > $now[1]) { // birthday month has not hit this year
        $dif -= 1;
    }
    elseif ($dob[1] == $now[1]) { // birthday month is this month, check day
        if ($dob[2] > $now[2]) {
            $dif -= 1;
        }
        elseif ($dob[2] == $now[2]) { // Happy Birthday!
            $dif = $dif." Happy Birthday!";
        };
    };
    return $dif;
}

getAge('1980-02-29');

But if you just simply want to know the age and nothing more, then:

function getAge($date) { // Y-m-d format
    return intval(substr(date('Ymd') - date('Ymd', strtotime($date)), 0, -4));
}

getAge('1980-02-29');

See BLOG


A key note about the strtotime method:

Note:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the 
separator between the various components: if the separator is a slash (/), 
then the American m/d/y is assumed; whereas if the separator is a dash (-) 
or a dot (.), then the European d-m-y format is assumed. If, however, the 
year is given in a two digit format and the separator is a dash (-, the date 
string is parsed as y-m-d.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or 
DateTime::createFromFormat() when possible.

Some web applications are needed to show the age of the user. In that case, you need to calculate the user age from date of birth. Here we’ll provide a short PHP code snippet to calculate age from date of birth.

In the following code, date(), date_create(), and date_diff() functions are used to calculate age of the user till today in PHP.

$dateOfBirth "17-10-1985";
$today date("Y-m-d");
$diff date_diff(date_create($dateOfBirth), date_create($today));
echo 
'Age is '.$diff->format('%y');

Use the above code to get the age from date of birth using PHP date functions.

Last update on August 19 2022 21:50:30 (UTC/GMT +8 hours)

PHP date: Exercise-18 with Solution

Write a PHP script to calculate the current age of a person.

Sample date of birth : 11.4.1987

Sample Solution:

PHP Code:

diff($bday);
printf(' Your age : %d years, %d month, %d days', $diff->y, $diff->m, $diff->d);
printf("\n");
?>

Sample Output:

 Your age : 30 years, 3 month, 0 days

N.B.: The result may varry for your system date and time.

Flowchart :

How to print dob in php?

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP function to get start and end date of a week (by week number) of a particular year.
Next: Write a PHP script to calculate weeks between two dates.

PHP: Tips of the Day

PHP: Creating default object from empty value in PHP?

Your new environment may have E_STRICT warnings enabled in error_reporting for PHP versions <= 5.3.x, or simply have error_reporting set to at least E_WARNING with PHP versions >= 5.4. That error is triggered when $res is NULL or not yet initialized:

$res = NULL;
$res->success = false; // Warning: Creating default object from empty value

PHP will report a different error message if $res is already initialized to some value but is not an object:

$res = 33;
$res->success = false; // Warning: Attempt to assign property of non-object

In order to comply with E_STRICT standards prior to PHP 5.4, or the normal E_WARNING error level in PHP >= 5.4, assuming you are trying to create a generic object and assign the property success, you need to declare $res as an object of stdClass in the global namespace:

$res = new \stdClass();
$res->success = false;

Ref : https://bit.ly/3mPEvSn

How to show date of birth in php?

$dateOfBirth = "17-10-1985"; $today = date("Y-m-d");

How to create age calculator in php?

php // PHP program to calculate age in years // Define the date of birth $dateOfBirth = '20-04-1988'; // Get today's date $now = date("Y-m-d"); // Calculate the time difference between the two dates $diff = date_diff(date_create($dateOfBirth), date_create($now)); // Get the age in years, months and days echo "your ...

How do I calculate age in HTML?

function calculateAge(date) { const now = new Date(); const diff = Math. abs(now - date ); const age = Math. floor(diff / (1000 * 60 * 60 * 24 * 365)); return age } var picker = new Pikaday({ field: document.

How do you convert a date of birth to an age in Excel?

How to calculate age from date of birth in Excel.
The formula is obvious and easy-to-remember, however, there is a tiny problem. ... .
=INT((TODAY()-B2)/365).
=YEARFRAC(B2, TODAY(), 1).
=ROUNDDOWN(YEARFRAC(B2, TODAY(), 1), 0).
=DATEDIF(B2, TODAY(), "y").