0

I currently have problem regarding incorrect date and time inserting on my database, I currently use laravel and in my app.php the 'timezone' => 'Canada/Central', today I insert an order which my time and date is 11:23PM / 09/06/2019 then after I insert the order in database the order_date is incorrect based on my time.

My Time and Date:

enter image description here

Order date and time Inserted in database:

order_date

I will share to you guys my script and code:

$now = new DateTime();

DB::insert('INSERT INTO order_properties (customer_id,order_ship_address,delivery_status,order_ship_province,order_date,transaction_number) 
        VALUES (?,?,?,?,?,?) ',[

        $hidden_customer_id,
        $hidden_customer_address,
        'Processing',
        Auth::user()->manager_location_assign,
        $now,
        $sessionTransactionNumber


    ]);
DevGe
  • 1,381
  • 4
  • 35
  • 66
  • did you try by clearing config cache. just run `php artisan config:clear` as well as clear cache `php artisan cache:clear`. most of the time whenever we change something in configuration. we need to restart our server (`php artisan serve`). so laravel know about new changes – Karan Sadana Jun 10 '19 at 05:47
  • yes I already did that on our server. but i got wrong time and date. – DevGe Jun 10 '19 at 05:48
  • also instead of using php datetime you can use `'now'` in its place if your column format is datetime or timestamp. also check your mysql timezone [here](https://stackoverflow.com/questions/930900/how-do-i-set-the-time-zone-of-mysql) – danish-khan-I Jun 10 '19 at 05:49
  • I insert today 11:52PM then in my order_date column its 6:00:15 – DevGe Jun 10 '19 at 06:01

1 Answers1

1

Make sure you have used the timezone during setting time. Else preferred to use the Eloquent for the data insert.

PHP DateTime class would not read the laravel settings automatically.

Also I would suggest to keep timezone in .env file so it could be accessible across the application. Here is what you can do for this.

  1. Add new key APP_TIMEZONE in .env

    APP_TIMEZONE="America/Chicago" //change as per your timezone.

  2. Update config/app.php to use .env settings.

    "timezone" = env("APP_TIMEZONE", "UTC");

  3. You must use the Carbon library and change code to fetch timezone like

    $now = Carbon::now(env("APP_TIMEZONE"));

Also make sure your timezone Canada/Central must be available in php supported list.

Here is the timezone supported by PHP https://www.w3schools.com/php/php_ref_timezones.asp

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vikash Pathak
  • 3,444
  • 1
  • 18
  • 32
  • do you know how to use multiple app timezone,?? i need 3 timezone in the same time – DevGe Jun 10 '19 at 06:46
  • i use calgary,winnipeg,edmonton timezone – DevGe Jun 10 '19 at 06:47
  • @DevGe would you like to show the same date in three different timezone? what is your actual requirement. – Vikash Pathak Jun 10 '19 at 06:49
  • that 3 province has different time and date, so if i'm in edmonton then my configuration in timezone is for winnipeg, so it different to edmonton. – DevGe Jun 10 '19 at 06:52
  • so how it may use the timezone of all city or province in canada. – DevGe Jun 10 '19 at 06:53
  • there is an conflict happen if i set one timezone in my configuration – DevGe Jun 10 '19 at 06:54
  • In such case better to save timezone in `UTC` in database so it would be consistent for all in application. And where you need to display the tmezone just parse that as per user current timezone. – Vikash Pathak Jun 10 '19 at 06:55
  • Also I suggest to detect the usertimezone using javascript and capture in session... that will used to parse the timezone for display using Laravel Accessor. https://laravel.com/docs/5.8/eloquent-mutators#accessors-and-mutators – Vikash Pathak Jun 10 '19 at 06:57
  • @DevGe let me know if it helps. – Vikash Pathak Jun 10 '19 at 09:40
  • sorry , yeah it good suggestion but you give me idea about storing timezone of user in database. thanks anyway. – DevGe Jun 11 '19 at 06:37