0

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 :)

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.