Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Corys8646's avatar

Determine Date of Birth from dates/ages

Hello,

Been thinking about how to best accomplish this for a few days but haven't come up with anything..hoping someone might have a suggestion.

I'm looking to create a class/function where I pass an array of dates and ages and have it return suggested date of birth. I know I can't determine the exact date of birth, but hoping to get something close.

Example would be Jane who is born on June 4th 1976

supplied array would be something like

2018-07-31 => 42
2018-06-01 => 41
2017-09-01 => 41
2017-07-10 => 41
2017-02-04 => 40
2016-06-05 => 40
2016-05-30 => 39

expected result would be 2018-06-02

In 2018 we see she had a birthday between June 1 and July 31, but in 2016 she had birthday between May 30th and June 5th. So we know she was born between June 1 and June 5th.

Any suggestions?

0 likes
4 replies
bobbybouwmann's avatar

This is not just a simple date method anymore :P

To make it easier I would probably start collecting some information first. You can for example determine first the average age of the given dates. Based on that you can already termine the correct year right.

To get the average date (day + month) I would probably first set the same year for all dates. Then I would start determining the difference between the days of these dates. Based on this you can also determine an average date just like the years.

I don't have any code right now, but I can help you brainstorm this idea ;)

Corys8646's avatar

@JLRDW - Right, she only has one birthday :) you can narrow the date down by looking at the ranges in the different years, then look at the overlap. The overlap in the years 2016 and 2018 are June 1- June 5.

Thanks for the link, I will take a read through that.

Corys8646's avatar

Thought more about this today and think I came up with a solution. If I normalize all the dates to a common year, and adjust the ages accordingly (change 2016 dates to 2018, and add two years to the age). I will get a "snapshot" of a single year, then be able to pluck whichever date the actual age change happened.

This code is a bit of a trainwreck right now, but seems to be working.

 $collection = collect([
        [
            'date' => \Carbon\Carbon::parse('2018-07-31'),
            'age' => 42
        ],
        [
            'date' => \Carbon\Carbon::parse('2017-09-01'),
            'age' => 41
        ],
        [
            'date' => \Carbon\Carbon::parse('2016-05-30'),
            'age' => 39
        ],
        [
            'date' => \Carbon\Carbon::parse('2016-06-05'),
            'age' => 40
        ],
        [
            'date' => \Carbon\Carbon::parse('2018-06-01'),
            'age' => 41
        ],
    ]);
    $target = 2018;
    
    $normalized=$collection->transform(function ($d) use ($target) {
        $increment = $target - $d['date']->format('Y');
        $d['date']->addYear($increment);
        $d['age'] = $d['age'] + $increment;
        return $d;
    })->sortBy('age')->sortBy('date');

    $starting_age = $normalized->first()['age'];
    $birthday=$normalized->whereNotIn('age',$starting_age)->first();
    
    dd($birthday['date']->subDay(1));

Please or to participate in this conversation.