In Laravel, you can use the Carbon
library to easily convert UTC time to local time. Carbon
is an extension of the DateTime class in PHP and provides convenient methods for working with dates and times.
Example 1:
Explore a 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('2024-01-02 21:20:48')
->setTimezone('Asia/Kolkata')
->toDateTimeString();
dd($time);
}
}
Output:
2024-01-03 02:50:48
Example 2:
Explore a 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:
2024-01-02 21:24:10