Laravel set timestamp timezone - laravel

Laravel has a timestamp that generates columns created_at and updated_at.
I want to ask what's the best way to set application timezone ? I want to set time zone for my application to GMT+8.

Change timezone value in config/app.php like :
'timezone' => 'Asia/Kathmandu',
I've added Asia/Kathmandu in context of Nepal. You can add your Time Zone over there.
Note: Don't forget to clear configuration cache.

Step 1: open file config/app.php
Step 2: find keyword: 'timezone' => 'UTC'
Step 3: Replace UTC to your timezone
Example: 'timezone' => 'Asia/Ho_Chi_Minh'
Find your timezone at here https://www.w3schools.com/php/php_ref_timezones.asp

Related

Change timezone of function date() in Laravel

Using this function in laravel date('d-m-Y_H.i.s') I get the date in the expected format but I'm not able to change the timezone in order to get my current time. It's one hour delayed.
I have already tried to change timezone unsuccessfully in app.php 'timezone' => 'Europe/Madrid';
Also tried in Illuminate/Foundation/Bootstrap/LoadConfiguration.php date_default_timezone_set($config->get('app.timezone', 'Europe/Madrid'));
Thanks in advance :)
You have two options:
Option1: Change UTC to user's timezone on frontend.
Option2: Change on backend.
How to manage timezone in Laravel

Understand Laravel's timezone

I'm trying to understand the following.
On my config/app.php I'm having my local timezone set to:
'timezone' => 'Europe/Athens'
My Postgres is set to:
SHOW timezone; // returns UTC
In the inserted rows in Postgres i would expect to see my local timezone, converted to UTC. Instead, the timestamp is written on my local timezone.
In another Node.js application, in which I'm working on, with another Postgres instance, when i insert a new row, the local timezone is converted to UTC while the timestamp is stored.
Why this is different on my Laravel app?
What is the case here?

How carbon's timezone are working? How to get date time based on specified timezone?

I'm struggling with the Carbon::now(), I'm specifying the desired timezone in the now function and it is not giving the date time based on the given timezone. It always using the UTC timezone that's specified in the laravel.
Carbon::now() // giving me date time of UTC timezone
Carbon::now('Asia/Karachi') // still giving me the date time of UTC timezone
var_dump(Carbon::now('Asia/Karachi')) // I'm able to see the correct date time based on the timezone.
So when I use the var_dump() I'm getting the correct date time based on the give timezone. Can anyone explain why it is nog returning the correct timezone from the Carbon::now('Asia/Karachi')? but dumping the instance showing the correct datetime.
I've also tried the php artisan cache:clear as well as php artisan config:clear
In config/app.php file change UTC to your desired timezone. It will change app timezone globally.
Eg:
'timezone' => 'Asia/Dhaka',
It always using the UTC timezone
Where? If you're talking about a JSON output, it's expected, date is formatted with ISO-8601 string and a trailing Z meaning Zulu = GMT timezone. The browser or client reading this JSON will have no issue to reconvert it then to any local date time using the user device timezone.
But at anytime if you dump explicitly this instance to a string using any format, the specified timezone will be used:
Carbon::now('Asia/Karachi')->format('Y-m-d H:i:s.u p')
(here using p or e you will see explicitly the timezone in the string.

How to change timezone using date() and strtotime() in Laravel

I'm still new in Laravel. I have a problem in understanding the concept of Carbon and the format for date and time. Everytime I create new post, the created_at column does not show the exact time as in my location. For example, currently in Kuala Lumpur the time is 2:36 PM but in my phpMyAdmin, it shows 6:36 AM. How can I change the timezone? And I'm using this code <span> {{date('d/m/Y g:i A', strtotime($comment->created_at))}} </span>
Here is what I already tried.
in my view blade.php
<span> {{ date('d/m/Y g:i A', strtotime($comment->created_at))->setTimezone('Asia/Kuala_Lumpur')}} </span>
but it gives an error Call to a member function setTimezone() on string
comments table
created_at - datetime
Hope anyone can teach and show me the correct one. Thank you.
You can set your app time zone by configuring app.php file in config folder .
To change time zone , modify the value of timezone in app.php file.
This is written in this section
|--------------------------------------------------------------------------
| 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.
|
For me i am using Asia/Kuala_Lumpur as my application time zone.
Here is the appropriate syntax :
'timezone' => 'Asia/Kuala_Lumpur'
list of timezones for PHP
NOTE : run php artisan config:clear to effect this changes

How do I tell DataMapper to use "timestamp with time zone" for DateTime properties?

By default, DataMapper creates DateTime properties of type timestamp without time zone in PostgreSQL. I'd like to change that default for my project to timestamp with time zone. How can this be done?
Just set ENV['TZ'] = 'your timezone'
just in case some jruby users pass by:
you need to set ENV['TZ'] = 'your timezone' and
org.joda.time.DateTimeZone.setDefault(org.joda.time.DateTimeZone.forID('your timezone'))
'UTC' works fine for me. for other timezone you might need to dig into joda-time.

Resources