0

I got json response with the following format :

 "/Date(1234)/"

How do I get the numbers only as string (1234) in php ?

thanks

3
  • If you have JSON, you need to use json_decode() Commented Jun 13, 2017 at 11:00
  • It's not a valid json string Commented Jun 13, 2017 at 11:02
  • Post the actual JSON data that you've received. Commented Jun 13, 2017 at 11:02

3 Answers 3

1
$dateonly = "/Date(1234)/";
echo $dateonly = preg_replace("/[^0-9,.]/", "",$dateonly);
Sign up to request clarification or add additional context in comments.

Comments

0

I don't think this is JSON, or at least not a valid one. Nonetheless, you can extract numbers like this:

preg_match_all('!\d+!', stripslashes("/Date(1234)/"), $output);
echo $output;

Comments

0

This format /Date(1234)/ usually called JSON Date Format. It contain unixtime with miliseconds. So, when you extract the number you need to divide it to 1000 to get the unixtime and process it in PHP. Here I give a function to extract the date and convert it to PHP DateTime Object

function parseJSDate($jsDateObject)
{
    $dateTime = null;
    if (preg_match("/\/Date\((\d+)\)\//", $jsDateObject, $match)) {
        if (isset($match[1]) && is_numeric($match[1])) {
            $timestamp = (int) $match[1];
            $dateTime  = new \DateTime();
            $dateTime->setTimestamp($timestamp / 1000);
        }
    }

    return $dateTime;
}

$date = parseJSDate("/Date(1224043200000)/");
echo $date->format("Y-m-d H:i:s");

Comments

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.