Sam Street
Sam Street

Reputation: 306

JSON Date Range From jQuery Calendar

I have a scenario where by I select a date from a jQuery Calendar and then, via ajax, iterate over a JSON file. My issue arises due to the use of date ranges, for example, see startdate and enddate:

    { "campus" : "A", "periods": [
    { "startdate" : "2013-01-02",
      "enddate" : "2013-01-06",
      "labels" : [
        { "Wednesday": 
            { "00:00" : "Closed",
              "09:00" : "Open",
              "18:00" : "Closed"
            },
          "Thursday": 
            { "00:00" : "Closed",
              "09:00" : "Open",
              "18:00" : "Closed"
            },
          "Friday": 
            { "00:00" : "Closed",
              "09:00" : "Open",
              "18:00" : "Closed"
            },
          "Saturday": 
            { "00:00" : "Closed"            
            },
          "Sunday": 
            { "00:00" : "Closed"            
            }
        }]
    },
    { "startdate" : "2013-01-07",
      "enddate" : "2013-03-24",
      "labels" : [
        { "Monday": 
            { "00:00" : "Closed",
              "09:00" : "Open",
              "18:00" : "Closed"
            },
          "Tuesday": 
            { "00:00" : "Closed",
              "09:00" : "Open",
              "18:00" : "Closed"
            },
          "Wednesday": 
            { "00:00" : "Closed",
              "09:00" : "Open",
              "18:00" : "Closed"
            },
          "Thursday": 
            { "00:00" : "Closed",
              "09:00" : "Open",
              "18:00" : "Closed"
            },
          "Friday": 
            { "00:00" : "Closed",
              "09:00" : "Open",
              "18:00" : "Closed"
            },
          "Saturday": 
            { "00:00" : "Closed"            
            },
          "Sunday": 
            { "00:00" : "Closed"            
            }
        }]
    }

As you can see from the snippet above, the startdate and enddate are a range. I was wondering what the best approach would be to try and select a date from within a range inside the JSON file.

I have considered using a PHP explode to break up the date that is passed to the ajax and then returned to the original page by doing a comparison between the exploded parameters and the data within the startdate and enddate. I Wondered if exploding the date and then iterating between the startdate and enddate to see if there is a match on the date would work?

If my above idea wouldn't work what other ways would be possible?

Upvotes: 1

Views: 1090

Answers (1)

user428517
user428517

Reputation: 4193

Why don't you try your idea? It sounds like a good start. But you can save some effort by not using explode. PHP has some useful built-in date/time functions, particularly strtotime, which converts a string date (of practically any format) to a Unix timestamp. This is just an integer which can be easily compared with other timestamps. Take a look:

$startdate;       #read this in from the json, contains e.g. '2013-01-02'
$enddate;         #read this in from the json, contains e.g. '2013-01-06'

#we want to check if this date is within the range
$datetomatch = '2013-01-04';

#get integer timestamps for each date
$starttime = strtotime($startdate);
$endtime = strtotime($enddate);
$matchtime = strtotime($datetomatch);

#check if $matchtime is within the range
if($matchtime >= $starttime && $matchtime <= $endtime) {
    #datetomatch is between $startdate and $enddate
}

So, the if statement would be true for the first object in your JSON periods array because 2013-01-04 is between 2013-01-02 and 2013-01-06 (or more specifically, because the timestamp 1357279200 is between 1357106400 and 1357452000). It would not be true for the second object in periods.

Of course, you can wrap this code in a function if you want to use it to check multiple dates.

Upvotes: 4

Related Questions