Search code examples
timezonelumen

Laravel lumen is not saving right time zone


Everyone I am new to Laravel Lumen Framework.I am using Laravel Lumen 5.4.7 and my PHP version is 7.0.I am getting the issue while saving time zone in database using Laravel Lumen web api.I have changed my local time zone in .env file

APP_TIMEZONE=Asia/karachi

When i just echo this code

date('Y-m-d H:i:s')

or this

\Carbon\Carbon::now()

both lines of code returns me my current local time but when i save this timezone in database for "created_at" and "updated_at" fields.It saves wrong time zone.Why this is happening?I am using this line of code to save data in database

$order_data=array(
        'amount'=>35,
        'updated_at' =>\Carbon\Carbon::now(),
        'created_at' =>\Carbon\Carbon::now()
    );
DB::table('orders')->insertGetId($order_data);

I have also tried this line of code to save data

DB::table('orders')->insert($order_data);

But still got wrong time zone in database in created_at and updated_at field.Then i also updated my code to use Elequont,for this purpose i have created a model named "Order" but still got wrong time zone in database.My Elequont code is following.

$order_data=array(
        'amount'=>35,
        'updated_at' =>\Carbon\Carbon::now(),
        'created_at' =>\Carbon\Carbon::now()
    );   
Order::create($order_data);

I have tried too much to remove this issue.But not get any luck yet.Can anyone help me why Lumen is not saving current time zone in database?


Solution

  • Try adding 'timezone' => env('APP_TIMEZONE', "UTC") to your config/app.php and running php artisan cache:clear after that.

    Also try adding DB_TIMEZONE=+05:00 to your .env file (adjusted to your UTC offset of course)