Home » Php » How to get local time in php?

How to get local time in php?

Posted by: admin December 2, 2021 Leave a comment

Questions:

I am trying to get the local time using php. I wrote two different versions, but they both give the wrong time

date_default_timezone_set('UTC');
$now = new DateTime();
echo $now->getTimestamp(); 

Another way

date_default_timezone_set('America/New York');
echo strtotime("now")."<br/>";;
$now = new DateTime();
echo $now->getTimestamp(); 

In both cases I get the time 4 fours ahead of my local time. There is any other way to get the local time?

Answers:

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();

###

I’m wondering why nobody has mentioned localtime(time()); in PHP with indexed key array result or localtime(time(), true); with associative key array result.

###

If you don’t want to use timezone you can use this mix of windows and php commands:

<?php 
  $D = exec('date /T');
  $T = exec('time /T');
  $DT = strtotime(str_replace("/","-",$D." ".$T));
  echo(date("Y-m-d H:i:s",$DT));
?>

###

If anyone here wanted to get the local time according to the user’s timezone (dynamically), then you can consider the following code:

$ip = $_SERVER['REMOTE_ADDR'];
$ipInfo = file_get_contents('http://ip-api.com/json/' . $ip);
$ipInfo = json_decode($ipInfo);
$timezone = $ipInfo->timezone ?? "UTC";

$dt = new DateTime("now", new DateTimeZone($timezone));
echo $dt->format('Y-m-d H:i:s');

The above code will get the user’s local timezone and print the time according to that.

If you wanted to test the above code on your localhost then make sure to hard code your IP address there because the API can’t provide you the timezone based on your local address (127.0.0.1), but the above code works fine on the
live server.

For Localhost, do:

$ip = '-------'; //your IP address

###

You can solve this problem by using localtime() function or through date_default_timezone_set() function.

Learn more about the localtime() function reffer:
http://php.net/manual/en/function.localtime.php

or

Learn more about the date_default_timezone_set() function reffer http://www.php.net/manual/en/function.date-default-timezone-set.php

i think this must help you..

###

For the record, I found the only solution to read the real hour of the machine is to read the information outside of PHP (javascript, shell or other processes). Why?

For example, let’s say we have an hour based in daily-saving. What if the timezone of the OS (Windows in my case) is not in sync with the timezone of PHP, I mean, it could be calculated differently. Or maybe the machine is using a different hour and it is ignoring the daily-saving hour.

Right now, my zone is UTC -4 Santiago 9:35 pm (with daily saving). However, PHP considers as 10:35 PM (using localtime,time,date and DateTime).