views:

51

answers:

1

Hi,

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

+1  A: 

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

hookedonwinter
@hookedonwinter: Thanks for you reply but, the date is 02/15/1982.For me today the json object was like - {"DateOfBirth":"\/Date(382597200000-0500)\/"} again this is 02/15/1982. I did discuss with the .NET developer of the webservice, he says like .NET it self parses the date for him and creates the JSON object for him. So it seems .NET is doing some specific conversion!!! let me add .NET TAG, if some one might know. Thanks for your help though.
jtanmay
In your comment you have the value 382597200000, but in your question you have 387518400000. You say that both are 02-15-1982, but I believe like @hookedonwinter that there is an error in the date specified in the question. hookedonwinter's solution works well if this is the case, so I'm voting it up.
Kevin
@kevin: I believe as its a timestamp, number would change every day. The number I asked 387518400000-0400, was yesterday. Please let me know if I am wrong. @hookedonwinter: Your logic did work for me except, if I do set the timezone, as you are doing in your code, it gives me 04-12-192, BUT if I don't specify the timezone it gives me 02-15-1982. Currently I am in EST timezone. I don't have much experience on datetime. But your suggestion did work for me. Thanks a lot. Marking as the correct answer.
jtanmay
Well... That's just strange. But hey, it works ::applies band-aid::
hookedonwinter
@jtanmay - The whole thing doesn't seem like a timestamp. The first 9 digits of the first part are, which is why hookedonwinter's solution pulls these out. I'd venture a guess that the "-0500" part is the time zone offset, but I don't know what the other 3 digits are for. With regards to timezone, since php 5.1.0 you should always have a default set if you are going to be using php functions (see http://www.php.net/manual/en/function.date-default-timezone-set.php), so you might want to consider investigating that further at some point.
Kevin
I was testing in my IDE, which... well, it's not a perfect environment :)
hookedonwinter
@Kevin: you are right its not timestamp. The developer for .NET told me yesterday he had some bug with his code 2 days back. 382597200000-0500 will be returned for 02/15/1982 always. And he is not sure with the trailing part!!! I would sure be looking into datetime much dipper in coming days. Thanks guys for all your help.
jtanmay
@hookedonwinter, @Kevin: Here are some of my findings with this number 382597200000-0500. The -0500 is the timezoneoffset (Which I dont have to worry in my code). The FIRST part is basically a timestamp but in milliseconds (This timestamp is given by javascript using getTime() function). So just to give heads up, instead of using the first 9 digits, one can grab all the digits and convert it to seconds to get the date from it. This can be useful if someone is working with timestamp between Javascript and php or I guess .NET is also giving milliseconds but not sure.
jtanmay
@jtanmay so the question is: are you all set?
hookedonwinter
@hookedonwinter, yes I am all set. Thanks to you guys :)
jtanmay