In this tutorial im going to learn how to change the TimeZone in laravel. How to set timezone in carbon laravel. if you want to see an example of set timezone in carbon laravel please stay this tutorial.
Step 1: using setTimezone()
Go to your Controller file and put below code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$time = Carbon::now()->setTimezone("Asia/Kolkata");
dd($time);
}
}
Output:
2023-05-23 18:06:42.217710 Asia/Kolkata (+05:30)
Second Example : – using tz()
Go to this file -> app/Http/Controllers/DemoController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
class DemoController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$time = Carbon::createFromFormat('Y-m-d H:i:s', '2023-05-23 05:01:01')->tz("Asia/Kolkata");
dd($time);
}
}
Output :-
2023-05-23 10:31:01.0 Asia/Kolkata (+05:30)
I hope its helpful for you.