简体   繁体   中英

Calculating age group from a given date of birth

In an application, I had a select box that looked like this:

<select name="AgeGroup" class="form-control" id="AgeGroup">
    <option value="18-24" selected=""18-24</option>
    <option value="18-24">18-24 years</option>
    <option value="25-34">25-34 years</option>
    <option value="35-44">35-44 years</option>
    <option value="45-54">45-54 years</option>
    <option value="55-64">55-64 years</option>
    <option value="65 Plus">65 years or over</option>
    <option value="PTNA">Prefer not to answer</option>
</select>

Further to this I also ask for a user's date of birth, but it seemed silly to ask the user for both, as surely you could work out the given age group from the date of birth provided?

As I'm collecting date of birth I have a simple mutator to get the user's age in years which looks like this:

/**
 * Calculate the user's age in years given their date of birth
 *
 * @return void
 */
public function getAgeAttribute()
{
    $this->birth_date->diff(Carbon::now())->format('Y');
}

Then I realised I don't even need to age attribute to work out the age group so I made another accessor like this:

/**
 * Infer the users age group given their date of birth 
 *
 * @return void
 */
public function getAgeGroupAttribute()
{
    $age = $this->birth_date->diff(Carbon::now())->format('Y');

    switch($age){
        case($age <= 24);
            return "18 - 24";
        break;
        case ($age <= 34);
            return "25 - 34";
        break;
        case ($age <= 44);
            return "35 - 44";
        break;
        case ($age <= 54);
            return "45 - 54";
        break;
        case ($age <= 64);
            return "55 - 64";
        break;
        case ($age > 64);
            return "Over 65";
        break;
        default:
            return "Unspecified age group";
    }
}

But my concern is, what if they haven't actually chosen to provide an age? As this form comes with an option of Prefer not to say.

Would I just check that this is actually a date before I did $user->age_group ?

Also, I suppose the first switch case should have an or because you could be younger than 18.

Like this: case($age >= 18 && $age <= 24);

You could just store prefer not to answer as a null value for their date of birth. Then when it comes to checking the user's age group you can check for a null value and return your prefer not to answer or unspecified option in your accessor:

public function getAgeGroupAttribute()
{
    if ($this->birth_date === null) {
        return 'Unspecified';
    }

    $age = $this->birth_date->diff(Carbon::now())->format('Y');

    // ...
}

You can also put the value 0 instead of PTNA

 <option value="0">Prefer not to answer</option>

and use your switch case with the case

case($age >= 18 && $age <= 24);

I would also change the default message in this situation just for consistency reasons, but this will do the trick.

Of course a more robust way is to check what is the value you are receiving, if it's not an age then not even put it inside switch case and redirect it to an else statement but the above solution is just quick and simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM