0

I am getting string after performing some queries in database like

 objectString = 'O:8:"stdClass":1:{s:5:"$date";O:8:"stdClass":1:{s:11:"$numberLong";s:13:"1546297200000";}}

I want to get $numberLong value from stdClass string. I have tried

      var dateObj = new Date(objectString.$date.$numberLong - 1000);

It throws error message at this line

"Uncaught TypeError: Cannot read property '$numberLong' of undefined at getDateStringObject"

Please help !!!

DeadLock
  • 448
  • 3
  • 11
Nida Amin
  • 735
  • 1
  • 8
  • 28

2 Answers2

1

This looks like a serialised stdObject. If you are getting this value from PHP at any point I would suggest unserializing it before returning it back to the javascript.

If done correctly you would be able to access the date as follows:

var dateObj = new Date(object.date.numberLong - 1000);

If you can't use PHP to unserialise the object, then you could use regex to match the value like so:

var objectString = 'O:8:"stdClass":1:{s:5:"$date";O:8:"stdClass":1:{s:11:"$numberLong";s:13:"1546297200000";}}'
var pattern = /("\$numberLong")(.+)("\d+")/g
var match = pattern.exec(objectString);
console.log(match[3]);
Jim Wright
  • 5,905
  • 1
  • 15
  • 34
0

objectString looks like a serialized object (see PHP serialize).

You will need to unserialize it into a PHP object in order be able to use it's properties (like $numberLong)

You can take a look at How to use php serialize() and unserialize() for somewhat of a simple explanation.

DeadLock
  • 448
  • 3
  • 11
  • How do i call php unserialize function and pass javascript variable "objectSting" as a parameter inside – Nida Amin Jun 18 '18 at 12:03
  • Your problem looks like a combination of this question and the one you ask here - https://stackoverflow.com/questions/50906080/javascript-stops-working-after-php-tag. Do you want the answer here or on that question? – DeadLock Jun 18 '18 at 12:10
  • There is already and answer there that should help. - Do what is recommended there. Update your code to this - $("#session_start_date").val(getDateString()); – DeadLock Jun 18 '18 at 12:12
  • You will need to do 1 more thing. In the javascript function `getDateString`, you will also need to do this - `str = JSON.parse(str); `. This will convert the string to a javascript object that you can use to fetch the `date` and `longNumber` values. – DeadLock Jun 18 '18 at 12:16