代码之家  ›  专栏  ›  技术社区  ›  Chris L

将函数存储在数据库中

  •  0
  • Chris L  · 技术社区  · 4 年前

    我将公共假日存储在一个数组中

        $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));
    

    还有更多。。。

    我想把它们放在一张桌子里,因为我想增加更多的国家。这是个好主意吗? 我想这样储存:

            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();
        });
    

    但我该如何计算日期呢? 再说一遍:这是个好主意吗?

    0 回复  |  直到 4 年前
        1
  •  1
  •   Álvaro González    4 年前

    你可能需要两者,它解析到的实际日期和用于计算它的算法 holiday 类似表格:

    id 假日类型id 假日_日期
    101 1. 2022-01-01
    102 2. 2022-11-27

    …与 holiday_type 表,其中包含以下定义:

    id country_id 名称 白天 表示
    1. 123 新年 1. 1. NULL
    2. 123 Lorem Ipsum NULL NULL 十一月的最后一个星期四

    表达式将是一个字符串,可通过 strtotime() / new \DateTime() 这个 syntax 非常强大,因此您可能不需要其他信息。然后,您可以创建一个函数来进行实际计算,并为所需的每一行调用(在cron作业中或使用手动脚本):

    public function calculateForYear(int $year): \DateTime
    {
    }
    
        2
  •  0
  •   Gazmend Sahiti    4 年前

    在_days之后添加另一列(例如,在其中保存“39”),然后在查询数据库之后可以执行此操作

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

    假设年份是静态的,并且不需要将其保存在数据库中。

    推荐文章