Home > Software engineering >  Store functions in database
Store functions in database

Time:03-01

I have public holidays stored in an array

    $holiday["Christi Himmelfahrt"]       = strtotime(" 39 day",easter_date($year));
    $holiday["Pfingstmontag"]             = strtotime(" 50 day",easter_date($year));
    $holiday["Fronleichnam"]              = strtotime(" 60 day",easter_date($year));

and some more...

I was thinking about storing them in a table, because I want to add more countries. Is this a good idea? I was thinking to store it like this:

        Schema::create('holidays', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->text('calculation');
        $table->string('country',2)->default('AT');
        $table->foreign('country')->references('iso_code')->on('countries');
        $table->timestamps();
    });

But how would I get this calculated dates? And again: is this a good idea at all?

CodePudding user response:

create new table

name country country_name country_id

CodePudding user response:

What about adding another column after_days where you save "39" for example and after you query the database you can do this

$holiday = Holiday::select('after_days')->where('id, '=', $id)->first();
strtotime(" ".$holiday->after_days." day", easter_date($year));

Assuming that the year is static and you don't need to save it in database.

CodePudding user response:

You probably need both, the actual date it resolves to and the algorithm used to calculate it. You can have calculated days in a holiday table like:

id holiday_type_id holiday_date
101 1 2022-01-01
102 2 2022-11-27

... together with a holiday_type table that contains the definitions:

id country_id name month day expression
1 123 New Year 1 1 NULL
2 123 Lorem Ipsum NULL NULL last thursday of november

The expression would be a string that's parseable by strtotime() / new \DateTime(). The syntax is quite powerful so you won't possibly need additional information. You can then create a function to make the actual calculations and invoke for every row you need (either in a cron job or with a manual script):

public function calculateForYear(int $year): \DateTime
{
}
  • Related