'storing datetime modifications + formats

I have a requirement where the user wants to be able to save "Date Modifier" strings to a database, and then use that with other dates in their application.

e.g. -

id   name              date_modifier
1    Add 1 Year        +1 year
2    Last Day Of Month last day of this month

// simplified; using DateTime
echo $date->modify($date_modifier);
// today = 02/03/2022
// Add 1 Year = 02/03/2023
// Last day of month = 31/03/2023

Essentially I'm passing a date modifier string to a DateTime object to modify it.

This seems to be working OK but I am running into issues -

For example; some of the requirements are 5th of {whatever date is supplied} this isn't working / I don't know what a date modifier looks like to achieve this.

$date = '02/03/2022';
$date->modify($fifth_of_month);
// outputs 05/03/2022

My next thought was to also store date_format so I wouldn't modify this date; but I would format it as $date->format('Y-m-05')

Is there a better approach to this as this looks like it could get very messy very quickly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source