How to Convert UTC Time to Local Time in Laravel?
Hello Friends,
Here, I will show you how to work laravel convert utc to local time. we will help you to give an example of laravel carbon convert utc to local time. I explained simply about how to convert utc time to local time in laravel. I’m going to show you about how to convert utc to local time in laravel. So, let's follow a few steps to create an example of convert utc to local time laravel.
we will use Carbon setTimezone() method to convert utc time to local time in laravel. you need to add your local timezone as argument in setTimezone() method. in this example, we will convert utc time to my local time "Asia/Kolkata".
So, let's see the simple examples with code:
Example 1:
you can see the simple example with custom date and time:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$time = Carbon::parse('2023-10-18 20:25:48')
->setTimezone('Asia/Kolkata')
->toDateTimeString();
dd($time);
}
}
Output:
2023-10-19 01:55:48
Example 2:
you can see the simple example with now date and time:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$time = Carbon::now()
->setTimezone('Asia/Kolkata')
->toDateTimeString();
dd($time);
}
}
Output:
2023-10-19 09:23:56
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Carbon Count Days Between Two Dates Example
- Laravel Carbon Parse Date Format Example
- How to Check Date is Today's Date or not in Laravel Carbon?
- Laravel Carbon Time Format AM PM Example Code
- How to use Carbon in Laravel Blade or Controller File?
- Laravel Carbon diffForHumans() Example
- How to Compare Two Dates in Laravel Carbon?
- Laravel Carbon Get Previous Month Example
- Laravel Carbon Get Next Month Example
- Laravel Carbon Get Tomorrow Date Example
- Laravel Carbon Get Yesterday Date Example
- Laravel Carbon Subtract Months from Date Example
- Laravel Carbon Add Days to Date Example