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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?