我想将一个字符串拆分为两个变量

I have selected two dates in a date range picker and stored them in one data base column but it only stores one date, like: 0000-00-00. I can't store two dates in one column so I want to split this string into two variable by hyphen (-).

This is my date range function:

<script>
   $(function () {

       //Initialize Select2 Elements
     $('.select2').select2();

     //Date range picker
     $('#bilty_date_range').daterangepicker();

     });
 </script>

This is my date insert code:

'bilty_date_range'      => date('y-m-d',strtotime($bilty_date_range)),

I am selecting the date like this: enter image description here

The backend result is like this: 11/07/2018 - 12/25/2018 .

String split functions are available in both Javascript and PHP.

In php, you can just use

$arr = explode("-", $str);

$date_start = $arr[0];
$date_end = $arr[1];

As a matter of fact, you don't even need to store both into two seperate variables after you got them in an array. Just pass $arr[0] and $arr[1] when storing them into Database.

i.e.,

'date_start'      => date('y-m-d',strtotime($arr[0])),
'date_end'      => date('y-m-d',strtotime($arr[1])),
var mystr = '11/07/2018 - 12/25/2018';

//Splitting it with " - " as the separator
var myarr = mystr.split(" - ");


var dateA = myarr[0];  // '11/07/2018'
var dateB = myarr[1];  // '12/25/2018'

Use PHP function explode to split the string, then create a valid date using DateTime interface from the string to store it as date type in the database not a string!

<?php 

$dates = '11/07/2018 - 12/25/2018';
$d = explode('-', $dates);

$dt1 = new DateTime($d[0]);
$dt2 = new DateTime($d[1]);

print_r($dt1);
print_r($dt2);

echo $dt1->format('Y-m-d');
echo PHP_EOL;
echo $dt2->format('Y-m-d');

DEMO: https://eval.in/1063328

Output:

DateTime Object
(
    [date] => 2018-11-07 00:00:00.000000
    [timezone_type] => 3
    [timezone] => UTC
)
DateTime Object
(
    [date] => 2018-12-25 00:00:00.000000
    [timezone_type] => 3
    [timezone] => UTC
)
2018-11-07
2018-12-25

if you want to split it via MySQL you can use SUBSTRING_INDEX like this:

MariaDB [(none)]> SET @d := '11/07/2018 - 12/25/2018';
Query OK, 0 rows affected (0.000 sec)

MariaDB [(none)]>
MariaDB [(none)]>
MariaDB [(none)]> SELECT
    -> SUBSTRING_INDEX(@d, '-', 1)  AS date_from,
    -> SUBSTRING_INDEX(@d, '-', -1) AS date_to;
+-------------+-------------+
| date_from   | date_to     |
+-------------+-------------+
| 11/07/2018  |  12/25/2018 |
+-------------+-------------+
1 row in set (0.000 sec)

MariaDB [(none)]>