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

InspiredPrynce's avatar

Calculating retirement date

I have a program where there are staff records, it has a date of first appointment and also a date of birth. There are two modes of retirement in the civil service, if you have served upto 35 years, you will retire, but if your date of first appointment + 35 years is more than 60years of your age, you will have to retire when you are 60, how do i do this calculation please?

someone help

0 likes
23 replies
Sinnbeck's avatar

I am not quite sure how that math would work?

If you take any date and add 35 years to it, it will by definition never be more than 35 years (eg. not over 60). Do you mean 60% of your age?

Can you show a very basic example of the math you need?

Like

years_since_first_appointment + age > 60
InspiredPrynce's avatar
InspiredPrynce
OP
Best Answer
Level 4

@sinnbeck this is the logic i need

$projected_year_of_retirement_based_on_years_of_first_appt = $years_since_first_appointment + 35;
if( by the time $projected_year_of_retirement_based_on_years_of_first_appt you are more than 60 years already)
    $you_new_retirement_date will now be 60years - $your_current_age, then add the result to your $years_since_first_appointment
but if($projected_year_of_retirement_based_on_years_of_first_appt falls before you are 60 years old)
    $you_new_retirement_date will now be $projected_year_of_retirement_based_on_years_of_first_appt 
jlrdw's avatar

but if your date of first appointment + 35 years is more than 60years of your age, you will have to retire when you are 60

Just curious, Bob joins the company at age 59. Are you saying Bob can retire in just one year of service to the company.

InspiredPrynce's avatar

yeah, exactly, but if Bob joins when he was 20 years old, he will retire at 55, which is 35 years of service

willvincent's avatar

If you have the logic you need.. why can't you turn that into the appropriate code yourself? You'd already at least halfway to your solution.

InspiredPrynce's avatar

my brain is not processing information properly at the moment, i need someone to do that

jlrdw's avatar

This is real simple math.

And I wish I had known about this when I was 59 I could have retired in one year.

1 like
Snapey's avatar

Calculate date of retirement (start date + 35 years)

Calculate date at which person will be 60 (date of birth plus 60 years)

Take which of the two dates is earlier. Thats the retirement date.

Your rule is retire after 35 years of service, or age 60, whichever comes first

1 like
willvincent's avatar

my brain is not processing information properly at the moment, i need someone to do that

It may be that software development isn't a good fit for you if you get stymied by such basic logic... food for thought.

Sinnbeck's avatar

Carbon has a diffInYears helper to get you going (my guess would be that this is the hard math for you, and not the "add 35 to another number" part :))

$dbDate = \Carbon\Carbon::parse($firstAppointment);
$diffYears = \Carbon\Carbon::now()->diffInYears($dbDate);
burlresearch's avatar

This is pretty simple:

        $retire_appointment = Carbon::parse($first_appointment)->addYears(35);
        $retire_birth = Carbon::parse($retire_birth)->addYears(60);
        $retire = $retire_appointment->min($retire_birth);
Sinnbeck's avatar

I'll give you an example :) Hope I am understanding how it should work :)


$firstAppointment = Carbon::parse($record->first_appointment);
$diffAppointmentYears = Carbon::now()->diffInYears($firstAppointment);
$age = Carbon::parse($record->age);
$diffAgeYears = Carbon::now()->diffInYears($age);

if ($diffAgeYears >= 60 || diffAppointmentYears >= 35) {
    return 'Congratulations! You are now retired!!';
}

return 'Get back to work!!!';
InspiredPrynce's avatar

i love your example, i would be happy if it returned the date of retirement

jlrdw's avatar

@inspiredprynce couldn't you work with the example by fixing it adapting overcoming tweaking.

Many times an example is just that it may be a little off but should point you in the right direction.

Dalma's avatar

I use the standard calculation of dead + 7 days.

Snapey's avatar

I gave you the business logic earlier. You have had 2 days to think about it. Lets see your progress?

BTW, your retirement would have been one day ago if on my team.

3 likes
Snapey's avatar

its not possible to write it for you as you have not shared any code

willvincent's avatar

Short of someone commiting the functioning code to your codebase you've been given the solution in several ways -- and you even pseudocoded the logic for it yourself.

What were you actually expecting from people?

2 likes
InspiredPrynce's avatar

I finally got it working, the pseudocode i wrote was the solution i needed with a combination of the responses i got from here as well did the trick!

Thanks alot guys!

Please or to participate in this conversation.