简体   繁体   English

Laravel / Lumen中的自定义时区支持

[英]Custom timezone support in Laravel/Lumen

If I'm building a timezone aware Laravel (Lumen) app, where the user may not have control over their database timezone settings, but can provide my app with a custom timezone in the config. 如果我正在构建时区感知的Laravel(Lumen)应用,则用户可能无法控制其数据库时区设置,但可以在配置中为我的应用提供自定义时区。

I also need to be able to store timestamps in the database via a direct connection in a third party app (again remember the timezone may be incorrect in the database config) so I'm thinking a unix timestamp would be appropriate so I can use UNIX_TIMESTAMP(NOW()) in my third party direct connection. 我还需要能够通过第三方应用程序中的直接连接将时间戳记存储在数据库中(再次记住,时区在数据库配置中可能不正确),因此我认为unix时间戳记是合适的,因此我可以使用UNIX_TIMESTAMP(NOW())在我的第三方直接连接中。

  1. Should I be storing timestamps in unix integer format? 我应该以Unix整数格式存储时间戳吗?

  2. What is the best way to support this globally in Laravel since $table->timestamps(); $table->timestamps(); table- $table->timestamps();以来,在Laravel中全局支持此功能的最佳方法是什么? uses TIMESTAMP 使用TIMESTAMP

  3. How do I ensure date/times are automatically converted to the timezone specified in the Laravel config when I query the database to output json to an API? 查询数据库以将json输出到API时,如何确保日期/时间自动转换为Laravel配置中指定的时区?

Note: I am not looking to translate to multiple timezones, just the one in the .env 注意:我不希望翻译成多个时区,而只是.env中的一个。

Current .env 当前.env

APP_TIMEZONE=UTC

Current migration example 当前迁移示例

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSharesTable extends Migration
{
    public function up()
    {
        Schema::create('shares', function (Blueprint $table) {
            $table->increments('id');

            $table->string('url', 500);
            $table->string('description', 100);
            $table->integer('hits');
            $table->string('ip_address', 39);

            $table->timestamps();
        });
    }

Current Share model 当前共享模型

namespace App;

use Illuminate\Database\Eloquent\Model;


class Share extends Model
{

    protected $fillable = ['url', 'description', 'ip_address'];

    protected $hidden = ['ip_address'];
}

Current controller example 电流控制器示例

namespace App\Http\Controllers;

use App\Share;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class ShareController extends Controller
{

    /**
     * @SWG\Get(
     *     tags={"Shares"},
     *     path="/shares",
     *     summary="Finds all shares",
     *     description="Returns all shares ",
     *     @SWG\Response(
     *         response=200,
     *         description="A list of all shares"
     *     )
     * )
     */
    public function fetchAll()
    {

        return Share::get();
    }

You can use config\\app.php instead of .env 您可以使用config\\app.php而不是.env

config(['app.timezone' => $timezone]);

where $timezone is one of these: http://php.net/manual/en/timezones.php 其中$timezone是其中之一: http : //php.net/manual/en/timezones.php

Should I be storing timestamps in unix integer format? 我应该以Unix整数格式存储时间戳吗?

You could, but iso8601 is litterally an ISO standard, so why not that 可以,但是iso8601在某种程度上是ISO标准,所以为什么不这样做呢?

What is the best way to support this globally in Laravel since $table->timestamps(); 自$ table-> timestamps();以来,在Laravel中全局支持此功能的最佳方法是什么? uses TIMESTAMP 使用TIMESTAMP

Carbon is a nice way to work with timestamps, adding and substracting time. Carbon是处理时间戳,增加和减少时间的一种好方法。 Use my code above to change the timezones and use them in Carbon. 使用上面的代码更改时区,并在Carbon中使用它们。

How do I ensure date/times are automatically converted to the timezone specified in the Laravel config when I query the database to output json to an API? 查询数据库以将json输出到API时,如何确保日期/时间自动转换为Laravel配置中指定的时区?

That's what Laravel will handle for you if you set the timezone in a session that can be kept between requests or if you pass it alang as an argument every request. 如果您在可以在请求之间保留的会话中设置时区,或者如果您在每个请求中都将alang传递为参数,那么Laravel将为您处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM