First the data:
CREATE TABLE IF NOT EXISTS `calendar_events` (
`event_id` int(5) unsigned NOT NULL AUTO_INCREMENT,
`event_day` date DEFAULT NULL,
`event_day_to` date DEFAULT NULL,
`event_year` int(4) NOT NULL DEFAULT '0',
`event_time` varchar(7) NOT NULL DEFAULT '',
`event_time_to` varchar(7) DEFAULT NULL,
`event_title` varchar(200) NOT NULL DEFAULT '',
`event_desc` text NOT NULL,
`event_type` varchar(2) DEFAULT NULL,
PRIMARY KEY (`event_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `calendar_events`
--
INSERT INTO `calendar_events` (`event_id`, `event_day`, `event_day_to`, `event_year`, `event_time`, `event_time_to`, `event_title`, `event_desc`, `event_type`)
VALUES
(8, '2012-02-10', '2012-02-10', 0, '', NULL, 'Feb 10', '', NULL),
(5, '2012-02-21', '2012-02-23', 0, '', NULL, '21-23 range addition', '', NULL),
(4, '2012-02-15', '2012-02-18', 0, '', NULL, 'Febbb 18', 'Some stuff here as a description', NULL),
(7, '2012-02-12', '2012-02-12', 0, '', NULL, 'single date 12', '', NULL),
(9, '2012-02-28', '2012-02-28', 0, '', NULL, 'test 28Date', '', NULL),
(10, '2012-02-01', '2012-02-01', 0, '', NULL, 'feb1', '', NULL);
Query that makes the values available:
mysql_select_db($database_MySQLconn, $MySQLconn);
$query_rsDates = "SELECT calendar_events.event_day, calendar_events.event_id, calendar_events.event_title, calendar_events.event_desc, calendar_events.event_day_to FROM calendar_events";
$rsDates = mysql_query($query_rsDates, $MySQLconn) or die(mysql_error());
$row_rsDates = mysql_fetch_assoc($rsDates);
$totalRows_rsDates = mysql_num_rows($rsDates);
Next the code setting the values to an array:
$dates = array();
while ($row_rsDates = mysql_fetch_assoc($rsDates))
{
for($start_date = strtotime($row_rsDates['event_day']);
$start_date <= strtotime($row_rsDates['event_day_to']);
$start_date = $start_date + 24*3600)
{
$row_rsDates['event_day'] = date("Y-n-j", $start_date);
$dates[] = $row_rsDates;
}
}
echo json_encode($dates);
Resulting JSON:
[{"event_day":"2012-2-21","event_id":"5","event_title":"21-23 range addition","event_desc":"","event_day_to":"2012-02-23"},{"event_day":"2012-2-22","event_id":"5","event_title":"21-23 range addition","event_desc":"","event_day_to":"2012-02-23"},{"event_day":"2012-2-23","event_id":"5","event_title":"21-23 range addition","event_desc":"","event_day_to":"2012-02-23"},{"event_day":"2012-2-15","event_id":"4","event_title":"Febbb 18","event_desc":"Some stuff here as a description","event_day_to":"2012-02-18"},{"event_day":"2012-2-16","event_id":"4","event_title":"Febbb 18","event_desc":"Some stuff here as a description","event_day_to":"2012-02-18"},{"event_day":"2012-2-17","event_id":"4","event_title":"Febbb 18","event_desc":"Some stuff here as a description","event_day_to":"2012-02-18"},{"event_day":"2012-2-18","event_id":"4","event_title":"Febbb 18","event_desc":"Some stuff here as a description","event_day_to":"2012-02-18"},{"event_day":"2012-2-12","event_id":"7","event_title":"single date 12","event_desc":"","event_day_to":"2012-02-12"},{"event_day":"2012-2-28","event_id":"9","event_title":"test 28Date","event_desc":"","event_day_to":"2012-02-28"},{"event_day":"2012-2-1","event_id":"10","event_title":"feb1","event_desc":"","event_day_to":"2012-02-01"}]
The 10th of Feb does not show…
I can add a new single date (same start and end date) and these work perfectly. I can remove the 10th of Feb and reinsert it as a new record but it still fails to show…
Now let’s print_r the current array:
Array
(
[0] => Array
(
[event_day] => 2012-2-21
[event_id] => 5
[event_title] => 21-23 range addition
[event_desc] =>
[event_day_to] => 2012-02-23
)
[1] => Array
(
[event_day] => 2012-2-22
[event_id] => 5
[event_title] => 21-23 range addition
[event_desc] =>
[event_day_to] => 2012-02-23
)
[2] => Array
(
[event_day] => 2012-2-23
[event_id] => 5
[event_title] => 21-23 range addition
[event_desc] =>
[event_day_to] => 2012-02-23
)
[3] => Array
(
[event_day] => 2012-2-15
[event_id] => 4
[event_title] => Febbb 18
[event_desc] => Some stuff here as a description
[event_day_to] => 2012-02-18
)
[4] => Array
(
[event_day] => 2012-2-16
[event_id] => 4
[event_title] => Febbb 18
[event_desc] => Some stuff here as a description
[event_day_to] => 2012-02-18
)
[5] => Array
(
[event_day] => 2012-2-17
[event_id] => 4
[event_title] => Febbb 18
[event_desc] => Some stuff here as a description
[event_day_to] => 2012-02-18
)
[6] => Array
(
[event_day] => 2012-2-18
[event_id] => 4
[event_title] => Feb 18 single date
[event_desc] => Some stuff here as a description
[event_day_to] => 2012-02-18
)
[7] => Array
(
[event_day] => 2012-2-12
[event_id] => 7
[event_title] => single date 12
[event_desc] =>
[event_day_to] => 2012-02-12
)
[8] => Array
(
[event_day] => 2012-2-28
[event_id] => 9
[event_title] => test 28Date
[event_desc] =>
[event_day_to] => 2012-02-28
)
[9] => Array
(
[event_day] => 2012-2-1
[event_id] => 10
[event_title] => feb1
[event_desc] =>
[event_day_to] => 2012-02-01
)
)
The mysterious 10th of Feb is nowhere to be found…
Why would this one date not work as the others? There is other single date events such as Feb 1st through Feb 1st so that is not the problem. And, if the Feb 10th date is added again as a second record (new event_id of course) this new record is added to the array without issue.
Why will the first record containing Feb 10th not be processed to the array?
By calling
before the while loop, you remove the first row. Just drop that line and things will be ok.