在Laravel中动态更改TimeZone [英] Change TimeZone dynamically in laravel

查看:595
本文介绍了在Laravel中动态更改TimeZone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,存在时区(PT,CST等)的下拉列表,当管理员从下拉列表更改时区时,管理面板会从所选下拉列表中反映时区.
如何根据所选选项更改Config/app.php时区"(应用程序时区).

In my project there is drop-down of time zones(PT,CST etc), when Admin changes the timezone from drop-down the admin panel reflects the timezone from the selected drop down.
How to change Config/app.php "timezone"( Application Timezone) according to the selected option.

推荐答案

您可以使用Laravel帮助器功能config设置时区.但是,这只会影响您将收到的请求.

You can use Laravel helper function config to set timezone. However, this will affects only the request you will receive.

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

如果您的目标是更改时区一次并在每个请求上运行,那么如何将更改后的时区保存在数据库或文件中呢?然后,为数据库编写查询或在app/config.php中读取文件,并在文件中更改索引时区的值.

If your goal is to change once the timezone and run on every request then what about saving the changed timezone in DB or in a file. Then, write a query for DB or read a file in app/config.php and change the value of index timezone in a file.

例如(文件示例):

当您更改时区时,它会保存在文件中.

When you changed the timezone it saves in a file.

file_put_contents("path/to/file", $timezone);

而且,在app/config.php

$timezone= file_get_contents("path/to/file");
return [
 . . .   
    'timezone' => $timezone,
 . . .
]

这篇关于在Laravel中动态更改TimeZone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆