I try to match to arrays based on the dates. The first array is generated by a function (getDateRange), the second array comes from my Wordpress database.
function getDateRange($startDate, $endDate, $format="Y-m-d")
{
//Create output variable
$datesArray = array();
//Calculate number of days in the range
$total_days = round(abs(strtotime($endDate) - strtotime($startDate)) / 86400, 0) + 1;
if($total_days<0) { return false; }
//Populate array of weekdays and counts
for($day=0; $day<$total_days; $day++)
{
$datesArray[] = date($format, strtotime("{$startDate} + {$day} days"));
}
//Return results array
return $datesArray;
}
$sql = "SELECT date(datetime) AS date, SUM(amount) as amount FROM sales GROUP BY 1";
$results = $wpdb->get_results($sql, ARRAY_A);
// Generate the date range
$dateRange = getDateRange('2014-10-01', '2014-10-06');
foreach($dateRange as $date) {
echo $date . ' | ';
if (array_key_exists($date, $results)) {
echo 'OK';
} else {
echo '0';
}
echo '<br />';
}
With the code above I don't get matching values:
2014-10-01 | 0
2014-10-02 | 0
2014-10-03 | 0
2014-10-04 | 0
2014-10-05 | 0
2014-10-06 | 0
The desired result is:
2014-10-01 | 0
2014-10-02 | 0
2014-10-03 | OK
2014-10-04 | 0
2014-10-05 | OK
2014-10-06 | 0