Gianmarco Gagliardi
Gianmarco Gagliardi

Reputation: 512

manage timezones JS difference of two hours

frontend side I send axios this date away:

Mon Jun 13 2022 18:29:00 GMT+0200 (Ora legale dell’Europa centrale)

because the backend (laravel 8.x) side saves me the usual one as follows:

2022-06-13 16:29:00

I wish he would save me 18:29 and not 16:29

how can i handle this? i can't install libraries like moment.js

Upvotes: 0

Views: 47

Answers (2)

Gianmarco Gagliardi
Gianmarco Gagliardi

Reputation: 512

I solved as follows:

    return new Date(year, month - 1, day, hours, minutes, seconds).toString().slice(0, 24);

can you do better?

Upvotes: 0

Savlon
Savlon

Reputation: 789

This will likely be due to your app timezone being set to UTC. I personally would recommend storing everything in UTC and converting that for display purposes on the front end.

However, if you must store them in the same format, you'll need to set the timezone in config/app.php

/*
|--------------------------------------------------------------------------
| 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' => 'UTC', <--- Change this to your timezone eg: Africa/Cairo

Select the correct timezone from this list.

Timezones available for GMT+0200

Don't forget to run php artisan config:clear to ensure your timezone configuration changes are being used.

Upvotes: 2

Related Questions