1

I'm trying to convert from JSON string to PHP a DOB and it works but gives me the wrong DOB.

$url = "test.js";
$content = file_get_contents($url);
$json = json_decode($content, true);
$DOB = date('m/d/Y', preg_replace('/[^\d]/','', $json['Player'][BirthDt])/1000);

what get JSONJason is :

"BirthDt":"\/Date(-388094400000)\/"

in PHP i get this:04/19/1982 instead of 09/14/1957

any ideas???

1
  • Calculating it manually, 4/19/1982 is indeed the date represented by that timestamp. Commented Aug 1, 2012 at 14:01

1 Answer 1

2

Your regular expression is removing the negative sign, so it's becoming a date after the unix epoch (Jan 1st 1970), instead of before. Try something like this:

preg_match('/Date\((-?\d+)\)/', $json['Player']['BirthDt'], $matches);
$timestamp = $matches[1];
$DOB = date('m/d/Y', $timestamp / 1000);

You can see it working in the demo.

Sign up to request clarification or add additional context in comments.

2 Comments

@nickb :quick question, there's one DOB that keeps failing and no idea why. json output is: "BirthDt":"\/Date(103608000000)\/" so it should be the same as 04/14/1973 but PHP is showing 1 day before 04/13/1973 instead???
@nickb - hi.. it was the time zone. i chanegd to GMT and wotks now. sorry for the trouble and thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.