0

i am receving start and end dates from a proc in following format

Array
(
    [0] => Array
        (
            [min_created_date] => 2020-10-28 00:00:00.000
            [max_created_date] => 2020-11-11 00:00:00.000
        )

)

i have stored the values in below variables with the following outputs resepectively :

$start_range =$slider_range[0]['min_created_date'];
$start_range=substr($start_range, 0, strrpos($start_range, ' '));
$end_range=  $slider_range[0]['max_created_date'];
$end_range=substr($end_range, 0, strrpos($end_range, ' '));

2020-10-28 2020-11-11

now i need to pass these dates to a jQuery function , i want to store this date to js variable since few js opetaions have to be performed to these dates further , Below is how i am trying to pass the dates

$(function() {
    var startrange =    <?php echo $start_range;?>;
    var endrange    =  <?php echo $end_range;?>;
    console.log(startrange+'startrange');
    console.log(endrange+'endrange'); 
 });

I get following in console : 
1982startrange
1998endrange

please guide me how can i pass the dates to JS variables instead of <?php echo $date ?>

  • Your problem is that when you output the contents of your PHP variables they are interpreted as a mathematical expression, not a date string. Wrapping them in quotes like aliirfan did in his answer should do the trick of the correct transfer of values. However, you might want to read [this question](https://stackoverflow.com/questions/23740548/how-do-i-pass-variables-and-data-from-php-to-javascript) to determine if this is the right transfer approach for you. – El_Vanja Nov 11 '20 at 11:55

2 Answers2

0
  • You have no other way to publish PHP variables in JavaScript without echo.
  • A cleaner way to do it to assign PHP variables to JavaScript vars inside a <script> tag so that PHP echo are not everywhere in your JS code.

<script type="text/javascript">
    var startrange = '<?php echo $start_range; ?>';
    var endrange = '<?php echo $end_range; ?>';
</script>
aliirfaan
  • 156
  • 5
0

you have a lot of options, but i love unixtimestamp formats more ^^

PHP

$minCreatedDate = time();
$maxCreatedDate = strtotime('2020-11-11 00:00:00.000');

JS echoed in PHP file

var startrange =  new Date(<?= $minCreatedDate ?> * 1000);

Date instance contains a lot of funcs. like "getDay", ...

Engazan
  • 470
  • 1
  • 3
  • 16