Use DateTime to Get Local Time in PHP

In this quick php date example, I will show you, how to get local time in php using php date. Suppose I live in Asia/Dhaka city and I want to print the local time of Dhaka right now. How we can do it using php date function. If you don't know how to do it, then this example is for you.

I will use php datetime for getting local time. For getting local time with am and pm, firstly i will use DateTime to get current time and then i wll set DateTimezone and then finally i will use php format function to convert it like what we want. See the example code:

Example code of utc to local time php:

$dt = new DateTime('now', new DateTimezone('Asia/Dhaka'));
return $dt->format('F j, Y, g:i a');

 

If you run this code, it will give the output like:

 

time
October 20, 2021, 2:03 pm

 

Hope it can help you.

 

Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js