简体   繁体   English

PHP:获取当前区域的时间

[英]PHP: Get Time for Current Region

I have a timezone of the user(he chooses it from a list) I have a time in UTC(not current time) So I need something like GetTimeForRegion(time, timezone) for PHP. 我有用户的时区(他从列表中选择),我有UTC时间(不是当前时间),所以我需要类似PHP的GetTimeForRegion(时间,时区)。 Is there such functions or libraries or services? 是否有此类功能或库或服务?

you can use DateTime::setTimezone(). 您可以使用DateTime :: setTimezone()。 If your UTC date is an UNIX timestamp, you can use some code like this : 如果您的UTC日期是UNIX时间戳,则可以使用如下代码:

$date = new DateTime();
$date->setTimezone(new DateTimeZone('UTC'));
$date->setTimestamp(1297869844);
$date->setTimezone(new DateTimeZone('Europe/Paris'));

echo $date->format('Y-m-d H:i:s');
// Will print 2011-02-16 16:24:04

date('r') or date('c') may help you. date('r')或date('c')可能会为您提供帮助。

echo date('r') prints Thu, 16 Feb 2011 16:01:07 +0200 echo date('r') print Thu, 16 Feb 2011 16:01:07 +0200

echo date('c') prints 2011-02-16T16:01:07+02:00 echo date('c')打印2011-02-16T16:01:07+02:00

You need to look at the Date/Time API in PHP. 您需要查看PHP中的Date / Time API。 I strongly advise you to stay away of gmdate and older date functions in php. 我强烈建议您不要使用PHP中的gmdate和较早的日期函数。

In your case, you should ask the user for its Olson based time zone. 对于您的情况,您应该向用户询问基于Olson的时区。

The code of Artefact2 will do the trick. Artefact2的代码可以解决问题。

please write this instead : 请改为这样写:
$date = date("Ymd H:i:s" , time()); $ date = date(“ Ymd H:i:s”,time());

so Y it means year 所以Y代表年份
m means month m表示月份
d means day H get hours from 0 - 24 d表示天H从0到24获得小时数
h get hours from 0 to 12 i get minutes 我得到0到12的小时我得到几分钟
s get seconds s秒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM