Laravel 5 配置时区未在 postgres 连接上定义时区
Laravel 5 configure timezone not define the timezone at postgres connection
我有一个问题:您通常如何在 laravel 5 的网站上处理时区问题?
Postgres 是本地时间,而我笔记本的本地时区是巴西利亚。但是应用程序 config/app.php
设置为 UTC 但是 config/database.php
中的数据库配置没有规范,所以我传递了参数来查找 config("app.timezone")
即 UTC
.
然后在登录后我打印 JWT CustomClaims
并且巴西利亚的时区出现了。 [0.o]
运行在postgres客户端,我把时区对冲成UTC,搜索用户数据就正常了。所以config/database.php
中的这个参数不是求解
有什么建议吗?
粘贴 database.php 配置:
系统:Debian Jessie
~$ timedatectl
tosystems@debian:~$ timedatectl
Local time: Thu 2017-04-06 21:16:55 -03
Universal time: Fri 2017-04-07 00:16:55 UTC
RTC time: Thu 2017-04-06 21:16:56
Time zone: America/Sao_Paulo (-03, -0300)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: yes
DST active: no
Last DST change: DST ended at
Sat 2017-02-18 23:59:59 -02
Sat 2017-02-18 23:00:00 -03
Next DST change: DST begins (the clock jumps one hour forward) at
Sat 2017-10-14 23:59:59 -03
Sun 2017-10-15 01:00:00 -02
Warning: The system is configured to read the RTC time in the local time zone. This
mode can not be fully supported. It will create various problems with time
zone changes and daylight saving time adjustments. The RTC time is never updated,
it relies on external facilities to maintain it. If at all possible, use
RTC in UTC by calling 'timedatectl set-local-rtc 0'
config/app.php
/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/
'timezone' => 'UTC',
/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/
'locale' => 'en',
config/database.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'timezone' => config('app.timezone'),
'locale' => config('app.locale'),
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
我的时区现在已通过身份验证:
array:1 [
"user" => array:4 [
"id" => 2
"name" => "Francis Rodrigues"
"email" => "francis-01@tosystems.net"
"last_logged_in_at" => Carbon {#236
+"date": "2017-04-07 11:49:48.000000"
+"timezone_type": 1
+"timezone": "-03:00"
}
]
]
也许您必须设置 PHP 支持的时区。
如果我没有误解的话,在你的情况下,你试图将时区设置为巴西。
这应该有效:
'timezone' => 'America/Brasilia'
我在用户模型中的临时解决方案:
/**
* Setting UTC timezone with mutator.
*
* @param $value
* @return string
*/
public function getLastLoggedInAtAttribute($value)
{
$carbon = Carbon::createFromTimestampUTC(strtotime($value));
return $carbon->toDateTimeString() . $carbon->getTimezone()->getName();
}
UTC 时区输出:
array:1 [
"user" => array:4 [
"id" => 2
"name" => "Francis Rodrigues"
"email" => "francis-01@tosystems.net"
"last_logged_in_at" => "2017-04-07 18:04:31+00:00"
]
]
我有一个问题:您通常如何在 laravel 5 的网站上处理时区问题?
Postgres 是本地时间,而我笔记本的本地时区是巴西利亚。但是应用程序 config/app.php
设置为 UTC 但是 config/database.php
中的数据库配置没有规范,所以我传递了参数来查找 config("app.timezone")
即 UTC
.
然后在登录后我打印 JWT CustomClaims
并且巴西利亚的时区出现了。 [0.o]
运行在postgres客户端,我把时区对冲成UTC,搜索用户数据就正常了。所以config/database.php
中的这个参数不是求解
有什么建议吗?
粘贴 database.php 配置:
系统:Debian Jessie
~$ timedatectl
tosystems@debian:~$ timedatectl
Local time: Thu 2017-04-06 21:16:55 -03
Universal time: Fri 2017-04-07 00:16:55 UTC
RTC time: Thu 2017-04-06 21:16:56
Time zone: America/Sao_Paulo (-03, -0300)
NTP enabled: yes
NTP synchronized: no
RTC in local TZ: yes
DST active: no
Last DST change: DST ended at
Sat 2017-02-18 23:59:59 -02
Sat 2017-02-18 23:00:00 -03
Next DST change: DST begins (the clock jumps one hour forward) at
Sat 2017-10-14 23:59:59 -03
Sun 2017-10-15 01:00:00 -02
Warning: The system is configured to read the RTC time in the local time zone. This
mode can not be fully supported. It will create various problems with time
zone changes and daylight saving time adjustments. The RTC time is never updated,
it relies on external facilities to maintain it. If at all possible, use
RTC in UTC by calling 'timedatectl set-local-rtc 0'
config/app.php
/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| Here you may specify the default timezone for your application, which
| will be used by the PHP date and date-time functions. We have gone
| ahead and set this to a sensible default for you out of the box.
|
*/
'timezone' => 'UTC',
/*
|--------------------------------------------------------------------------
| Application Locale Configuration
|--------------------------------------------------------------------------
|
| The application locale determines the default locale that will be used
| by the translation service provider. You are free to set this value
| to any of the locales which will be supported by the application.
|
*/
'locale' => 'en',
config/database.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/
'default' => env('DB_CONNECTION', 'mysql'),
/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'timezone' => config('app.timezone'),
'locale' => config('app.locale'),
'prefix' => '',
'schema' => 'public',
'sslmode' => 'prefer',
],
],
我的时区现在已通过身份验证:
array:1 [
"user" => array:4 [
"id" => 2
"name" => "Francis Rodrigues"
"email" => "francis-01@tosystems.net"
"last_logged_in_at" => Carbon {#236
+"date": "2017-04-07 11:49:48.000000"
+"timezone_type": 1
+"timezone": "-03:00"
}
]
]
也许您必须设置 PHP 支持的时区。
如果我没有误解的话,在你的情况下,你试图将时区设置为巴西。
这应该有效:
'timezone' => 'America/Brasilia'
我在用户模型中的临时解决方案:
/**
* Setting UTC timezone with mutator.
*
* @param $value
* @return string
*/
public function getLastLoggedInAtAttribute($value)
{
$carbon = Carbon::createFromTimestampUTC(strtotime($value));
return $carbon->toDateTimeString() . $carbon->getTimezone()->getName();
}
UTC 时区输出:
array:1 [
"user" => array:4 [
"id" => 2
"name" => "Francis Rodrigues"
"email" => "francis-01@tosystems.net"
"last_logged_in_at" => "2017-04-07 18:04:31+00:00"
]
]