Updated April 11, 2023
Introduction to Laravel Timezone
The default timestamp in Laravel is saved in UTC and if the user is working on an application in a different zone, with timestamp in the application which doesn’t match the timezone they are present. To solve this, the user should configure the timezone saved in the database and convert the models related to the user in the applicable timezone from UTC. It can be achieved in two methods, one is retrieving and saving the timezone of the user and the other is changing the UTC timestamp to the timezone of the user.
What is the Laravel timezone?
James Mills launched a package called Laravel timezone which configures the timezone in the application for the user. It displays the time and date in the local timezone. It functions by listening to the login event of the user and setting the timezone in the database. It also uses GeoJP of the Laravel to look at the user who is using the concerned IP address. The user can display the local timezone to the user that provides the façade.
- Timezone:: convert to local ($post -> created)
The blade directive has specific syntax like,
@display date ($post -> created)
@display date ($post -> created "Y-M-D-G: i."true)
The user can also change the local date back to the UTC.
- Timezone:: convert from local ($request –> get(“publish_at’));
How to set laravel timezone?
As discussed earlier, the timezone in laravel can be configured in two ways.
To add a new column, create a PHP file first and it is saved in database -> migration -> 2022_21_02_0001_ create_users_table. Execute the following artisan code to fetch all the tables and re-execute the migration process. Then save the timezone at registration.
Laravel timezone config
In this step, the timezone of the user is retrieved at the time of the registration process. Run the script to install the timezone plugin and fetch the timezone of the user. Then fill the hidden fields in the given registration form. In this registration form, include the timezone as the hidden values. Then open the registration page from the menu and choose to inspect. There the timezone of the user can be viewed. Now fetch the timezone from the post request of registration. The use of register methods gets refreshed every time the new user is sign-in and registers. To enable this feature, the user can add the following code to the file.
App/HTTP/ controllers/ Authentication/ Registercontroller. Php file.
Now, when the user selects the register button after filling all the required fields, the timezone is saved in the database and it can be viewed and accessed whenever it is required. Similar to saving the data at registration, it can be done at the time of sign-in or log-in also. The same batch of the script is included at end of the file login.blade.php or signin.blade.php and add all the hidden fields in the application login form. Once the timezone is configured in the login form, the user can make an authentication method using the login controller to save the timezone of the user in the database. So once the user is logged in, and he doesn’t have any timezone prior in the database, this new timezone which he is logged in now, will be added to the database.
Now the user can handle both the sign-in and registration methods to work on the timezone in the database. In the next section, conversion of UTC timestamp to the timezone of user-specific is discussed.
By default, the timestamp of the Laravel is included in the place like updated. at, created.at fields in the sample tables which are saved in the timezone of the UTC and it is configured in the file, config/ app.php. The timezone is saved as UTC. The user can convert the data explicitly with the timezone of the user and the saved timezone field. The mutator of Laravel in this task can be defined every time, which is comprised of timestamp and fetched from the database which passes through the mutator to conversion.
For example, the conversion of created. at the field is displayed to the user as below,
public function get Created.At Attribute( $values)
$timezone = optional (authentication ()->user()) -> timezone ?? config ('application.timezone');
return value:: parse($value)->timezone ($timezone);
Laravel timezone local Automatically
Laravel enables the user to choose or change the timezone explicitly from the timezone list offered by the PHP. In the drop-down column, the user can select the timezone which needs to be saved in the database. The timezone field is fetched from the request and is used to save in the database which also helps to convert the user timestamp in UTC for future purposes.
User = Authvalue ::user ();
$user -> timezone = $request -> timezone;
$user -> store();
Hence, in this article, the conversion of UTC timestamps and configuration of timezone by the different users is discussed in brief which helps the user to imply it accordingly in the application.
We hope that this EDUCBA information on “Laravel Timezone” was beneficial to you. You can view EDUCBA’s recommended articles for more information.