Tomzie Tomzie - 5 months ago 10
PHP Question

PHP parsing specific date string

An

API
call is returning me a date as a string in the following format:

/Date(1358208000000)/


Now, I would like to parse this string into a date object (PHP).

I could use str_replace remove the /Date( and )/ and divide the unix timestamp with 1000, but that seems a bit complicated for this matter to me.

Any thoughts on how I could parse this easily to a date object?

Answer

As you said that you want easy way. Yo can use like below;-

<?php
    $str = '/Date(1358208000000)/';
    $int = filter_var($str, FILTER_SANITIZE_NUMBER_INT); // get the number only from the string
    echo date ('Y-m-d',($int/1000)); // convert number to seconds and get correct date
?>

Output:- https://eval.in/595171

Comments