简体   繁体   中英

How to correctly pass JSON from php to Javascript with Dates?

I am tinkering with the Jquery Weekly Calendar Plugin (Which you can look at the full demo here: skedyul.herokuapp.com)

Now, I'm trying to put some events dynamically using PHP but I'm fairly new to the concept of JSON.

This is my example php array to be encoded to JSON:

<?php 
        $events = array(
            array(
            "id" => 1,
            "start" => date("D M j Y H:i:s "),
            "end" =>  date("D M j Y H:i:s ",strtotime("+30 minutes")),
            "title" => "College Algebra"
            ),
            array(
            "id" => 2,
            "start" => date("D M j Y H:i:s ",strtotime("+30 minutes +1 day")),
            "end" =>  date("D M j Y H:i:s ",strtotime("+45 minutes +1 day")),
            "title" => "Calculus"
            )
        );
?>

And here is the working Javascript code with some sample data:

<script type="text/javascript">

        function getEventData() {
          var year = new Date().getFullYear();
          var month = new Date().getMonth();
          var day = new Date().getDate();
          console.log(new Date(year, month, day, 12));
          return {
             events : [
                {
                   "id":1,
                   "start": new Date(year, month, day, 12),
                   "end": new Date(year, month, day, 13, 30),
                   "title":"College Algebra",
                },
                {
                   "id":2,
                   "start": new Date(year, month, day, 14),
                   "end": new Date(year, month, day, 14, 45),
                   "title":"Statistics and Probability"
                }
             ]
          };
       }

And here, I changed the above Javascript into this (putting the json encoded php associative array)

<script type="text/javascript">

            function getEventData() {
              var year = new Date().getFullYear();
              var month = new Date().getMonth();
              var day = new Date().getDate();
              console.log(new Date(year, month, day, 12));
              return {
                 events : <?php echo json_encode($events)?>
              };
           }
</script>

It's seems to work an doesn't spews out errors but the problem is about the Date. It's not showing properly at the calendar..

How do I correctly pass JSON from PHP with dates? Thank you :)

Please consider: date.js from http://code.google.com/p/datejs
You can use Date.parse() or Date.parseExact() In your case, you use Date.parseExact(dateString, "d MMM d yyyy HH:mm:ss")

In your page, include the date.js.

<script src="http://yoursite/path/to/date.js"></script>

Then try the following...

var dateobj = Date.parseExact(dateString, "d MMM d yyyy HH:mm:ss")
alert(dateobj.toString());

Please read http://code.google.com/p/datejs/wiki/FormatSpecifiers for date formatting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM