php 如何在php中获取本地时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24914227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get local time in php?
提问by Alexander
I am trying to get the local time using php. I wrote two different versions, but they both give the wrong time
我正在尝试使用 php 获取本地时间。我写了两个不同的版本,但它们都给出了错误的时间
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?
在这两种情况下,我的时间都比我的当地时间早 4 分。还有其他方法可以获取本地时间吗?
回答by N.B.
DateTime::getTimestamp()
returns unix timestamp. That number is always UTC. What you want is format the date according to your time zone.
DateTime::getTimestamp()
返回unix时间戳。该数字始终为 UTC。您想要的是根据您的时区格式化日期。
$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.
此外,您可以使用 DateTime 库来满足您的所有日期需求。每次要获取日期时,无需更改服务器的默认时区。
回答by Shiplu
Simply use function date_default_timezone_set(). Here is example:
只需使用函数 date_default_timezone_set()。这是示例:
<?php
date_default_timezone_set("Asia/Dhaka");
echo date('d-m-Y h:i:s A');
?>
Hope it will help, Thanks.
希望它会有所帮助,谢谢。
回答by user2847376
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.
您需要使用 javascript,因为您需要来自客户端的时间价值。PHP 是一种服务器端语言,它在服务器上进行评估并将结果发送到客户端。因此,如果您尝试使用 PHP 函数提取日期/时间,它将从服务器提取日期,而该日期并不总是在客户端的时区中。
You can do this in a javascript function and then reference the value in the display.
您可以在 javascript 函数中执行此操作,然后在显示中引用该值。
var thisDateTime = new Date();
var thisDateTime = new Date();
回答by Hassan Azimi
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.
我想知道为什么没有人localtime(time());
在 PHP中提到索引键数组结果或localtime(time(), true);
关联键数组结果。
回答by Andrea Savojardo
If you don't want to use timezone you can use this mix of windows and php commands:
如果您不想使用时区,您可以使用以下 windows 和 php 命令的组合:
<?php
$D = exec('date /T');
$T = exec('time /T');
$DT = strtotime(str_replace("/","-",$D." ".$T));
echo(date("Y-m-d H:i:s",$DT));
?>
回答by magallanes
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?
为了记录,我发现读取机器真实小时的唯一解决方案是读取PHP之外的信息(javascript,shell或其他进程)。为什么?
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.
例如,假设我们有一个小时的日常储蓄。如果操作系统的时区(在我的情况下为 Windows)与 PHP 的时区不同步,我的意思是,它可以以不同的方式计算。或者,机器可能使用了不同的时间,而忽略了每天节省的时间。
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).
现在,我的区域是 UTC -4 Santiago 晚上 9:35(每日保存)。但是,PHP 认为是晚上 10:35(使用本地时间、时间、日期和日期时间)。
回答by Ajesh VC
You can solve this problem by using localtime()function or through date_default_timezone_set()function.
您可以使用localtime()函数或通过date_default_timezone_set()函数解决此问题。
Learn more about the localtime() function reffer:
http://php.net/manual/en/function.localtime.php
了解有关 localtime() 函数 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
了解有关 date_default_timezone_set() 函数引用的更多信息http://www.php.net/manual/en/function.date-default-timezone-set.php
i think this must help you..
我想这一定对你有帮助..