json date对象到php date [英] json date object to php date

查看:89
本文介绍了json date对象到php date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用一个webservice,它返回一个json对象。
json对象编码日期。我试图找到一种方法,将该日期转换为php中的m-d-Y格式。
Json对象是{DateOfBirth:/ Date(387518400000-0400)/}这个日期是02-15-1982。



webservice我打电话是在.NET中,它将日期转换为JSON对象。不确定是否有帮助。



提前感谢



谢谢,
Tanmay

解决方案

如果有机会说02-15-1982但是真的意味着04-12-1982,那么我有一个解决方案。如果没有,那么在大约60天的时间里有一个差距,这可以用更多的数学计算。



这是我现在的解决方案:

  date_default_timezone_set('America / Denver'); 

$ json = json_decode('{DateOfBirth:\ / Date(387518400000-0400)\ /}');
$ date_string = $ json - >出生日期;

preg_match('/([\d] {9})/',$ date_string,$ matches); //获取字符串中的前9位数字

echo date('m-d-Y',$ matches [0]);

返回: 04-12-1982


I am calling a webservice which return me back a json object. The json object encodes the date. I am trying to find a way to convert that date to m-d-Y format in php. Json object is {"DateOfBirth":"/Date(387518400000-0400)/"} this date is 02-15-1982.

The webservice which I am calling is in .NET, and it converts the date to the JSON object. Not sure if this would help.

Thanks in advance

Thanks, Tanmay

解决方案

If there's a chance you said 02-15-1982 but really meant 04-12-1982, then I have a solution. If not, then there's a gap in the time of about 60 days, which can be accounted for with a bit more math.

Here's my solution for now:

date_default_timezone_set(  'America/Denver' );

$json = json_decode( '{"DateOfBirth":"\/Date(387518400000-0400)\/"}' );
$date_string = $json -> DateOfBirth;

preg_match( '/([\d]{9})/', $date_string, $matches ); // gets just the first 9 digits in that string

echo date( 'm-d-Y', $matches[0] );

This returns: 04-12-1982

这篇关于json date对象到php date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆