【问题标题】:Laravel Lumen Query Return Wrong Timestamp TimeLaravel Lumen 查询返回错误的时间戳时间
【发布时间】:2020-11-23 10:56:23
【问题描述】:

我在 Ubuntu 服务器上有我的 Laravel Lumen 应用程序,我更改了服务器时区,我的应用程序使用正确的时区成功保存了 created_at、update_at,现在当我获取一些表数据时,我注意到结果时间不是与数据库中的相同。

例如:

  • 数据库中的created_at = 2020-08-03 13:52:35

  • 查询结果中的created_at = 2020-08-03 10:52:35

根据要求更新,以下是我的查询

 public function Outbox($id){
            
        $orders = DB::table('order_details as o')
                    ->join('user_profile as a','o.reciever_id','=','a.id')
                    ->join('regions as r','a.province','=','r.province')
                    ->where('o.sender_id' ,'=', $id)
                    ->select('a.bid','r.ar_name as province','o.order_bar','o.price','o.delivery_cost','o.description','o.type','o.dropoff_time as time','o.status','o.visual_status','o.created_at')
                    ->orderBy('o.created_at','desc')
                    ->get();
        if(count($orders) > 0){
                return response()->json(['status_code'=>1000,'data'=>$orders , 'message'=>null],200);
            }else{
                return response()->json(['status_code'=>2000,'data'=>null , 'message'=>null],200);
            }
    }

这里有什么问题??

任何帮助将不胜感激

【问题讨论】:

  • 检查“/config/app.php”中服务器和laravel应用程序的时区
  • Lumen 没有配置文件

标签: mysql laravel lumen


【解决方案1】:

在您的bootstrap/app.php 中检查您所在的时区

date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));

别忘了在.env 文件中添加APP_TIMEZONE

更新

如果您在 bootstrap/app.php 中没有找到该行,请在引导环境变量后添加它

<?php

require_once __DIR__.'/../vendor/autoload.php';

(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
    dirname(__DIR__)
))->bootstrap();

date_default_timezone_set(env('APP_TIMEZONE', 'UTC'));

并在.env 中添加

APP_TIMEZONE=UTC

【讨论】:

  • app.php中没有这样的时区行吗?
  • 您使用的 Lumen 版本是什么?
  • 流明 (5.8.12)。
  • 还是一样的结果,没有任何改变
  • 好的,与我们分享您的查询,以便我们了解发生了什么。
【解决方案2】:

我花了一些时间才发现,在 Lumen 中,您必须将其添加到您的 .env 文件中,例如:

DB_TIMEZONE="Europe/Warsaw"

这应该可以解决问题。

https://github.com/laravel/lumen-framework/blob/8.x/config/database.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 2020-07-03
    • 1970-01-01
    相关资源
    最近更新 更多