在php中使用时区偏移将GMT时间转换为本地时间
我需要根据当前时区显示用户的活动日期。 我的方法-在php中使用时区偏移将GMT时间转换为本地时间,php,timestamp-with-timezone,Php,Timestamp With Timezone,我需要根据当前时区显示用户的活动日期。 我的方法- 从javascript获取时区偏移量并将其存储到用户的配置文件表中 当用户登录时,获取时区偏移 当前日期与时区偏移正常工作- $offsetDiff=$_会话['TimeZone']*60 $UserDateTime=time()+$offsetDiff $currentDate=日期($Y-m-d',$UserDateTime) Dateo other today不能正常工作- $offsetDiff=$_会话['TimeZone']*60
有人能告诉我如何根据时区偏移显示正确的日期吗?您可以将特定偏移转换为日期时区:
$offset = '-0500';
$isDST = 1; // Daylight Saving 1 - on, 0 - off
$timezoneName = timezone_name_from_abbr('', intval($offset, 10) * 36, $isDST);
$timezone = new DateTimeZone($timezoneName);
然后您可以在DateTime构造函数中使用它,例如
$datetime = new DateTime('2012-04-21 01:13:30', $timezone);
或使用setter:
$datetime->setTimezone($timezone);
在后一种情况下,如果$datetime是用不同的时区构造的,则日期/时间将转换为指定的时区。为什么不使用对象并在实例化时将a传递给构造函数?感谢@hizbul25,但它给出了一个fatol错误“Catchable fatal error:datetime类的对象无法转换为”Ok中的字符串,试试这个,