代码之家  ›  专栏  ›  技术社区  ›  Javed

laravel:设置时间戳只返回自定义日期

  •  1
  • Javed  · 技术社区  · 7 年前

    在我的应用程序中,我刚刚设置 timezone 作为 UTC 当我返回响应时,我得到的日期格式为 UTC .

        "created_at": {
            "date": "2018-05-18 15:17:37.000000",
            "timezone_type": 3,
            "timezone": "UTC"
        },
        "updated_at": {
            "date": "2018-06-08 05:49:05.000000",
            "timezone_type": 3,
            "timezone": "UTC"
        }
    

    但我只需要 updated_at : 2018-06-08 05:49:05 ,我不需要 timzone type, timezone … 预期:

        "country"   : "United States Of America",
        "region"    : "Northeast",
        "updated_at": "2018-06-08 05:49:05"
    

    我该怎么做?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Tartar    7 年前

    看看这个

    https://laravel.com/docs/5.6/eloquent-mutators#date-mutators

    可以通过在模型中使用访问器方法和所需的时间格式来执行此操作。

    要方便地格式化日期和时间,请使用 Carbon

    https://carbon.nesbot.com/