Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sabbir345's avatar

Jquery Multiple date separate

"date":"05/01/2018 - 05/03/2018"

how to separate this string . I want to need like this - first = 05-01-2018 and second = 05-03-2018

0 likes
8 replies
campo's avatar
campo
Best Answer
Level 7

Are the dates always in this exact format? If so, you could simply just explode them into an array.

$dates = explode(' - ', $jsonObject->date);

// $dates[0] = 05-01-2018
// $dates[1] = 05-03-2018
1 like
tykus's avatar

Regex? You can pattern match the string into an array of the required values

/(\d{2}\/\d{2}\/\d{4})/g
1 like
Sabbir345's avatar

Here is my real data :

{"_token":"qaRK1dyrgJ94SVjXmhW4RwDg8qyIlStQj1xoTAUq","employeeId":"3","date":"05/01/2018 - 05/03/2018"}

@campo and @tykus

campo's avatar

You need to decode it to an object using json_decode e.g.

$json = json_decode('{"_token":"qaRK1dyrgJ94SVjXmhW4RwDg8qyIlStQj1xoTAUq","employeeId":"3","date":"05/01/2018 - 05/03/2018"}');

$dates = explode(' - ', $json->date);
tykus's avatar

Are we doing our work in PHP or Javascript here?

2 likes
campo's avatar

I didn't notice the title said JavaScript. facepalm

You could just split it in JS.

let dates = data.date.split(' - ');

// $dates[0] = 05-01-2018
// $dates[1] = 05-03-2018
1 like

Please or to participate in this conversation.