Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用时区偏移将GMT时间转换为本地时间_Php_Timestamp With Timezone - Fatal编程技术网

在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

我需要根据当前时区显示用户的活动日期。 我的方法-

  • 从javascript获取时区偏移量并将其存储到用户的配置文件表中
  • 当用户登录时,获取时区偏移
  • 当前日期与时区偏移正常工作-
  • $offsetDiff=$_会话['TimeZone']*60

    $UserDateTime=time()+$offsetDiff

    $currentDate=日期($Y-m-d',$UserDateTime)

  • Dateo other today不能正常工作-
  • $offsetDiff=$_会话['TimeZone']*60

    $UserDateTime='2014-02-10 08:58:00';+$抵销差额

    $monthUser=date('Y-m-d',$UserDateTime)


    有人能告诉我如何根据时区偏移显示正确的日期吗?

    您可以将特定偏移转换为日期时区:

    $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中的字符串,试试这个,