I have an array like the one below
array(
"hgfh" => array("s" => "2011-11-28T07:00-04:00", "e" => "2011-11-29T06:59-04:00"),
"hgfh" => array("s" => "2011-11-29T07:00-04:00", "e" => "2011-11-30T06:59-04:00"),
"hgfh" => array("s" => "2011-11-30T07:00-04:00", "e" => "2011-12-01T06:59-04:00"),
"hgfh" => array("s" => "2011-12-01T07:00-04:00", "e" => "2011-12-02T06:59-04:00"),
"hgf" => array("s" => "2011-12-02T07:00-04:00", "e" => "2011-12-05T06:59-04:00"),
"sog" => array("s" => "2011-12-05T07:00-04:00", "e" => "2011-12-06T06:59-04:00"),
"gfd" => array("s" => "2011-12-06T07:00-04:00", "e" => "2011-12-07T06:59-04:00"),
"gfd" => array("s" => "2011-12-07T07:00-04:00", "e" => "2011-12-08T06:59-04:00"),
"bob" => array("s" => "2011-12-08T07:00-04:00", "e" => "2011-12-09T06:59-04:00"),
"tree" => array("s" => "2011-12-09T07:00-04:00", "e" => "2011-12-11T23:00-04:00"),
);
For example and I need to determine if the current date("c"); is between the "s" and "e" in that array and then get the key. So it would say today is the 2011-11-30T09:50-04:00 and determine that the key is hgfh Is there any quick and dirty method to do so? I’m a bit worried about performance, so if there is a method that may be better performance wise, I would appreciate that.
Storing dates as strings, especially when you want to do such ‘between’ comparisons, will be painful no matter what you do. You should be storing the raw PHP timestamp values that represent those times, e.g.
(using obviously fake timestamp values). Then it becomes a simple matter of:
Otherwise you’re stuck converting those strings to time values every time you run the check loop, which gets expensive very fast. Always store times/dates in native formats, and convert to human-readable only as necessary.