Change timezone of function date() in Laravel - 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

Related

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.

Carbon isn't giving the correct datetime with setTimezone

I'm using Carbon inside of my Laravel API project, when trying to set the timezone and get a datetime back in the user's timezone, I'm for some reason getting a UTC value back, what am I doing wrong?
// $user->timezone will give me "America/Curacao" or whatever their time zone is
$curr = Carbon::now()->setTimezone($user->timezone)
But when I echo out the contents of $curr, I'm getting a UTC time of:
2021-04-05T11:58:35.186750Z
What am I missing?
You get the ISO-8601 string with is the standard format for dates in JSON. But the object is correctly in America/Curacao timezone. If you want to pass it to a Date object in the browser, you don't need the timezone and can just pass this string as is.
If you want to format the date to be seen by the user, then just pick the format after setting the timezone:
$curr = Carbon::now()->setTimezone($user->timezone)->format('Y-m-d H:i:s')
Or whatever format would be relevant for you.
And you should keep 'timezone' => 'UTC' in your app settings as your users will likely have any various timezone, and UTC is the more agnostic to use to save them back-end side.

Laravel 5.8 - Validate future date

How do I validate that a given date is in the future and not a past date? Alternatively, does Html 5 allows one to disable past date from a date field?
I have a form in my app that allows user to select start date for an event from an HTML date input field. I want users to only choose a date in the future and not a past date.
I think you can use it like this:
$rules = [
'start_date' => 'date_format:d/m/Y|after:3/13/2019',
];
See also the laravel documentation:
https://laravel.com/docs/5.8/validation#rule-after
or
https://laravel.com/docs/5.8/validation#rule-after-or-equal
after:today works as #Tim Lewis commented above.
As for HTML validation, date fields still aren't fully supported. Safari being the main culprit.
You can use min="2020-01-01" format against the input, but you'll need to either add that date server-side or using JavaScript so it's tomorrow's date.
I have tried code and found that if you want to validate only year than you can write something like
$rules = ['dob'=>'required|date_format:Y|before:today']
where,
date_format
is fixed keyword and with help of 'Y' you can validate only year
and before:today validate if year is older than today or not.
you can use d-m-Y format instead only Y.
happy coding...

Laravel set timestamp timezone

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

Resources