Skip to content
Advertisement

Spliting Daterange values of jquery in two PHP variables

I am having a daterangepicker in form which grabs value in this format 10/12/2020 - 10/16/2020 i am passing this value using ajax to a PHP page for storing in Database. But I want to split daterange in to two variable for example from_date and to_date. i.e

$from_date = 2020-10-12;
$to_date = 2020-10-16;

I am not able to split these values in this format

Advertisement

Answer

You can use explode() for this and for the formatting date() combined with strtotime():

    $t = explode(' - ','10/12/2020 - 10/16/2020');
    
    var_dump($t);
    // array(2) { [0]=> string(10) "10/12/2020" [1]=> string(10) "10/16/2020" }

    $from_date = date("Y-m-d", strtotime($t[0]));
    $to_date   = date("Y-m-d", strtotime($t[1]));

    echo $from_date;
    // 2020-10-12

Now you have

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement