如何从php获取日期值到jquery变量?

时间:2016-02-11 14:43:30

标签: php jquery html5 datepicker

<?php 
       $sql = "SELECT * FROM expense WHERE userid = $userid";

$expense_date= array();  //array declared to be used below

$amount = array();// array declared


$result = mysqli_query($conn, $sql);

 if(!$result){
      die("query failed"); // query failed due to no connection or error in query
  } else {
 while( $row = mysqli_fetch_assoc($result)){ // fetches infromation 

        array_push($amount, $row["amount"]); //pushes my distances whic are returned from the query from the database into an array

        $date_entered = ( date("d-n-y", strtotime($row["timestamp"])));

        array_push($expense_date, $date_entered);//pushes date travelled

      } 
  }

//$arr = array('distance'=>$dist_covered, 'dateTravel'=>$travel_date);
//print_r(json_encode($arr));    ------ make sure distance are being inserted into the database

    echo $date_entered; ?>

<script type="text/javascript">

var unavailableDates = <?php print_r(json_encode($date_entered))?>;

function unavailable(date) {

    dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();

    if($.inArray(dmy, unavailableDates) == -1) {

        return [true, ""];
    }
    else {
        return [false, "", "Unavailable"];
    }
}

$(function() {
    $( "#Datepicker1" ).datepicker({
        numberOfMonths:2,
        beforeShowDay: unavailable
    }); 
});
</script>

我试图让unavailableDates变量从数据库中选出一个日期。在php中的变量以相同的格式回显日期。但变量没有认识到它。如果我手动输入日期,则可以使用。

2 个答案:

答案 0 :(得分:3)

var unavailableDates = <?php echo json_encode($date_entered)); ?> || '';

print_r()和var_dump()都用于显示数据以进行调试。 print和echo用于输出字符串。使用回声是最常见的。

$d = date("d-n-y");
print_r($d);
var_dump($d);
echo $d;

将产生:

11-2-16
string(7) "11-2-16"
11-2-16

因此,即使您使用print_r()而不是echo,结果也是相同的输出。这是因为在这种情况下它是一个字符串变量。或者这是一系列日期?

您的代码中可能还有其他问题。你收到任何控制台错误吗?

我可能会遗漏一些东西。不可用日期应该是一组日期吗?在这种情况下,您可能会将变量混淆一些。请参阅array_push()PHP函数。

请参阅 http://php.net/manual/en/function.array-push.php

array_push($ expenseDates,$ date_entered); //推送行进日期

则...

var unavailableDates = <?php echo (!empty($expenseDates)) ? json_encode($expenseDates) || []; ?>;

答案 1 :(得分:0)

是的,说实话,你的代码有点乱,我在下面纠正了很多明显的问题:

<?php
$sql = "SELECT * FROM expense WHERE userid = $userid";
$expense_date = array(); //array declared to be used below
$amount = array(); // array declared
$result = mysqli_query($conn, $sql);
if (!$result) {
    die("query failed"); // query failed due to no connection or error in query
} else {
    while ($row = mysqli_fetch_assoc($result)) {
        $amount[] = $row["amount"];
        $date_entered = date("d-m-y", $row["timestamp"]); #CORRECTED - ONLY FEED IN THE TIMESTAMP 
        $expense_date[] = $date_entered;
    }
}
//echo $date_entered; #WHY IS THIS BEING ECHOED OUT HERE FROM PHP?
?>

<script type="text/javascript">
    var unavailableDates = '<?= $date_entered ?>';
    function unavailable(date) {
        dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
        if ($.inArray(dmy, unavailableDates) == -1) {
            return [true, ""];
        }
        else {
            return [false, "", "Unavailable"];
        }
    }
    $(function () {
        $("#Datepicker1").datepicker({
            numberOfMonths: 2,
            //beforeShowDay: unavailable # WHAT IS THIS?
            beforeShowDay: unavailable('MISSING_DATE')
        });
    });
</script>
  1. 这一切都发生在1 .php档案中吗?
  2. 查看datepicker代码 - 看起来是为了调用unavailable() .js函数?那要打电话的日期是什么日期?