How to get local time in php?

DateTime::getTimestamp() returns unix timestamp. That number is always UTC. What you want is format the date according to your time zone.

$dt = new DateTime("now", new DateTimeZone('America/New York'));

echo $dt->format('m/d/Y, H:i:s');

Or use a different date format, according to what you need.

Also, you can use DateTime library for all your date needs. No need to change server's default timezone every time you want to fetch the date.


Simply use function date_default_timezone_set(). Here is example:

<?php 
date_default_timezone_set("Asia/Dhaka");
echo date('d-m-Y h:i:s A');
?>

Hope it will help, Thanks.


You need to use javascript because you want the value of time from the client. PHP is a server-side language that evaluates on the server and sends results to the client. So if you try to pull a date/time using a PHP function, it's going to pull the date from the server, which is not always in the client's timezone.

You can do this in a javascript function and then reference the value in the display.

var thisDateTime = new Date();

Tags:

Php