【问题标题】:Configuring the Laravel timezone base on the system timezone in Laravel 5基于 Laravel 5 中的系统时区配置 Laravel 时区
【发布时间】:2019-04-28 11:19:58
【问题描述】:

我正在尝试根据我在 CentOS VM 上的系统时间自动设置 config/app.php 上的时区配置。

'timezone' => 'America/New_York',

我尝试在config/app.php 之上添加这几行代码

// Try #1
// $timeZone = shell_exec("date +\"%Z %z\" | awk '{print $1}'");
// dd($timeZone);  I got "EST\n"

// Try #2
// session_start();
// $timezone = $_SESSION['time'];
// dd($timeZone);

// Try #3
// $timezone = date_default_timezone_get()
// dd($timeZone);

以上这些似乎都不起作用。

如何进一步调试?

【问题讨论】:

标签: php laravel laravel-5 centos timezone


【解决方案1】:

你可以试试这个:

$now = new DateTime();
$tz = $now->getTimezone();
$tz = $tz->getName();

如果上述方法不起作用,我建议您查看Carbon

您的config/app.php 文件将类似于:

<?php
# include Carbon
use Carbon\Carbon;

return [
    . 
    .
    . 
    |--------------------------------------------------------------------------
    | 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' => Carbon::createFromTimestamp(0, 'America/New_York')->timezone->getName(),
    .
    . 
    .

];

要测试上面的输出,不要在你的app/config.php 中转储dd(),你可以创建一条新路由:

Route::get('/test-timezone', function() {
    dd(\Config::get('app.timezone'));
});

确保清除缓存以查看更改:php artisan config:cache

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    相关资源
    最近更新 更多