dpauf28808 2014-12-17 11:39
浏览 59
已采纳

如何将来自Web Service的Date值转换为PHP中的日期字符串? [重复]

This question already has an answer here:

I am querying some data from MSSQL server using a web service which I have written. I get the values in JSON format. In order to convert data table to json string I use System.Web.Script.Serialization.JavaScriptSerializer class. I have a date field which is turned into a long integer between the paranthesis.

...,"startDate":"\/Date(1387231200000)\/",...

When I try to convert this value to date string using gmdate() function,

$sDate = gmdate("d/m/Y", preg_replace('/\D/','',$jsonArray['startDate']));

I get 18/08/1968 result. But the actual value in DB is 17.12.2013 00:00:00.

I also tried

$sDate = gmdate("d/m/Y", "1387231200000"); 

still returns 18/08/1968.

</div>
  • 写回答

1条回答 默认 最新

  • dro60731 2014-12-17 13:15
    关注

    Finally I have foundt the solution. According to answer here which was deleted I could have got nearly correct value. As RiggsFolly mentioned, date string closes to desired value after deleted last three zeros.

    Then I have changed the time zone to Europe/Istanbul.

    $sDate = createDate($jsonArray['startDate']);
    

    And the function createDate() comes:

    function createDate($date) {
    
        $date = preg_replace('/\D/', '', substr($date, 0, -5));
    
        $desiredDateFormatString = "d/m/Y";
    
        $inputEpoch = preg_replace('/\D/', '', $date);
    
        $desiredTimeZoneString = 'Europe/Istanbul';
    
        $dateTimeObject = new DateTime(date('d.m.Y H:i:s', $inputEpoch));
    
        $dateTimeObject->setTimezone(new DateTimeZone($desiredTimeZoneString));  
    
        $outputDateTimeString = $dateTimeObject->format($desiredDateFormatString);
    
        return $outputDateTimeString;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作