London Smith London Smith - 14 days ago 6
PHP Question

Recover two dates which are betweens 2 brackets []

I got a string with two dates between 2 brackets and separated by a comma like that:

[2016-09-07],[2016-11-16]


How to recover these dates and put them in two variables,
$startDate
and
$endDate
?

Thanks.

Answer

Assuming you obtain as a string you can use explode

 $myStartString = "[2016-09-07],[2016-11-16]";

if you need you can remove the brackets this way

 $remove = ['[',']'];
 $myString = str_replace( $remove, "", myStartString);

then

 $myArray = explode(',' $myString);

 $startDate = $myArray[0];
 $endDate = $myArray[1];

Or wihthout the brackets:

$myString = '2016-09-07,2016-11-16';
$myArray = explode(',', $myString);
$startDate = $myArray[0];
$endDate = $myArray[1];
echo $startDate. ' '.$endDate ;

But is better if you have a normalized database