Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6365613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:18:54+00:00 2026-05-25T00:18:54+00:00

I am trying to calculate the nearest Age based on DOB, but i cant

  • 0

I am trying to calculate the nearest Age based on DOB, but i cant wrap my head around how to do it. I have tried some methods which estimates but this is not good enough. We need to calculate the days from today and the next birthday, whether it is in the current year or next year. and also calculate the days from today and the last birthday again whether it is in the current year or last year.

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:18:55+00:00Added an answer on May 25, 2026 at 12:18 am

    I think this is what you want…. of course, you could just get a persons age accurate to the day and round it up or down to the closest year….. which is probably what I should have done.

    It’s quite brute force, so I’m sure you can do it better, but what it does is check the number of days until this year’s, next year’s, and last year’s birthday (I checked each of those three separately instead of subtracting from 365, since date() takes care of leap years, and I don’t want to). Then it calculates age from whichever one of those birthdays is closest.

    Working example

    <?php
    $bday = "September 3, 1990";
    // Output is 21 on 2011-08-27 for 1990-09-03
    
    // Check the times until this, next, and last year's bdays
    $time_until = strtotime(date('M j', strtotime($bday))) - time();
    $this_year = abs($time_until);
    
    $time_until = strtotime(date('M j', strtotime($bday)).' +1 year') - time();
    $next_year = abs($time_until);
    
    $time_until = strtotime(date('M j', strtotime($bday)).' -1 year') - time();
    $last_year = abs($time_until);
    
    $years = array($this_year, $next_year, $last_year);
    
    // Calculate age based on closest bday
    if (min($years) == $this_year) {
        $age = date('Y', time()) - date('Y', strtotime($bday));
    }
    if (min($years) == $next_year) {
        $age = date('Y', strtotime('+1 year')) - date('Y', strtotime($bday));
    }
    if (min($years) == $last_year) {
        $age = date('Y', strtotime('-1 year')) - date('Y', strtotime($bday));
    }
    
    echo "You are $age years old.";
    ?>
    

    Edit: Removed unnecessary date()s in the $time_until calcs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to calculate if some pins I have are inside my screen.
I am trying to calculate the sub_total field based on: total_full + total_half. however
Im trying to calculate the bitrate from a video, but Im not sure if
I am trying to calculate sum of 'price' field of an 'observableArray'. I have
I'm trying to calculate the median of a set of values, but I don't
I am trying to calculate a SHA-1 Hash from a string, but when I
I'm trying to calculate some columns in a mysql database with this code: SELECT
i am trying calculate the amount but having troble with jQuery .. can you
I'm trying to calculate how many minutes have elapsed between a saved DateTime and
I am trying to calculate exponential moving average on 15 day bars, but want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.