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
Related
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
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.
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.
I have a persian datepicker in my view
<div class="input-group-addon" data-mddatetimepicker="true" data-targetselector="#exampleInput1" data-trigger="click" data-enabletimepicker="true">
<span class="glyphicon glyphicon-calendar"></span>
</div>
<input type="text" class="form-control" id="exampleInput1" name="start_at"/>
and i have a column in my table that in datetime format
when I send this to controller and dd($request['start_at']); it returns "۱۳۹۷/۰۷/۲۳ ۰۰:۰۰:۰۰"
How to convert this to datetime format to save in database?
Intro
Everything that is related to date or time in Laravel should be done with Carbon.
Carbon is a PHP Library/Extension to handle the formatting of Date and Time. You can find there docs over here. Carbonis already included in Laravel, so check the API out over here.
Getting things done
What you need to do is, pass in your timezone here:
$date = Carbon::now($start_at)->locale('UTC+6');
Assuming that the persia timezone is UTC+6the above snippet converts your time to a carbon time.
After this you can access your timestamp with
$date->timestamp;
Or your DateTimeString with
$date->toDateTimeString();
Please note: I am not absolutely sure about the time settings in persia. From a quick research I would assume that is UTC+6. You can also put in a string in the locale() function like:
$date = Carbon::now()->locale('fr_FR');
if this is easier to you!
Quickshot URLs:
Laravel Carbon API
Carbon API
use this package of laravel to convert date Persian to DateTime
laravel persian datetime package
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