Date: 2022-06-01 - # of leads: 1 Date: 2022-06-04 - # of leads: 1 Date: 2022-06-09 - # of leads: 1 Date: 2022-06-10 - # of leads: 1 Date: 2022-06-13 - # of leads: 1 Date: 2022-06-14 - # of leads: 2 Date: 2022-06-15 - # of leads: 1 Date: 2022-06-19 - # of leads: 3 Date: 2022-06-21 - # of leads: 1 Date: 2022-06-24 - # of leads: 1 Date: 2022-06-28 - # of leads: 2 Date: 2022-06-29 - # of leads: 1 Date: 2022-06-30 - # of leads: 2 ===Expected Output==== Date: 2022-06-01 - # of leads: 1 Date: 2022-06-02 - # of leads: 0 Date: 2022-06-03 - # of leads: 0 Date: 2022-06-04 - # of leads: 1 Date: 2022-06-05 - # of leads: 0 Date: 2022-06-06 - # of leads: 0 Date: 2022-06-07 - # of leads: 0 Date: 2022-06-08 - # of leads: 0 Date: 2022-06-09 - # of leads: 1 Date: 2022-06-10 - # of leads: 1 Date: 2022-06-11 - # of leads: 0 Date: 2022-06-12 - # of leads: 0 Date: 2022-06-13 - # of leads: 1 Date: 2022-06-14 - # of leads: 2 Date: 2022-06-15 - # of leads: 1 Date: 2022-06-16 - # of leads: 0 Date: 2022-06-17 - # of leads: 0 Date: 2022-06-18 - # of leads: 0 Date: 2022-06-19 - # of leads: 3 Date: 2022-06-20 - # of leads: 0 Date: 2022-06-21 - # of leads: 1 Date: 2022-06-22 - # of leads: 0 Date: 2022-06-23 - # of leads: 0 Date: 2022-06-24 - # of leads: 1 Date: 2022-06-25 - # of leads: 0 Date: 2022-06-26 - # of leads: 0 Date: 2022-06-27 - # of leads: 0 Date: 2022-06-28 - # of leads: 2 Date: 2022-06-29 - # of leads: 1 Date: 2022-06-30 - # of leads: 2
<?php
$month = date('m', strtotime('-1 month'));
$year = date("Y");
$start_date = "01-".$month."-".$year;
$start_time = strtotime($start_date);
$end_time = strtotime(" 1 month", $start_time);
for($i=$start_time; $i<$end_time; $i =86400){
$list[] = date('Y-m-d', $i);
}
foreach($dailyLeads as $dL) :
$date = date('Y-m-d', strtotime($dL['date_created']));
if(in_array($date, $list)){
echo 'Date: ' . $date . ' - # of leads: ' . $dL['today_leads'] . '<br />';
} else {
echo 'Date: ' . $list . ' - # of leads: 0 <br />';
}
endforeach;
?>
CodePudding user response:
Loop through all dates. If a day is missing in the dataset, output the leads with 0.
$dailyLeads = [
['date_created' => '2022-06-01', 'today_leads' => 1],
['date_created' => '2022-06-04', 'today_leads' => 1],
['date_created' => '2022-06-09', 'today_leads' => 1],
['date_created' => '2022-06-10', 'today_leads' => 1],
['date_created' => '2022-06-13', 'today_leads' => 1],
['date_created' => '2022-06-14', 'today_leads' => 2],
['date_created' => '2022-06-15', 'today_leads' => 1],
['date_created' => '2022-06-19', 'today_leads' => 3],
['date_created' => '2022-06-21', 'today_leads' => 1],
['date_created' => '2022-06-24', 'today_leads' => 1],
['date_created' => '2022-06-28', 'today_leads' => 2],
['date_created' => '2022-06-29', 'today_leads' => 1],
['date_created' => '2022-06-30', 'today_leads' => 2],
];
$year = 2022;
$month = 6;
$dayCount = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for($day = 1; $day <= $dayCount; $day ) {
$date = sprintf("d-d-d", $year, $month, $day);
$lead = current(array_filter($dailyLeads, fn($dailyLead) => $dailyLead['date_created'] === $date));
printf("Date: %s - # of leads: %d\n", $date, $lead['today_leads'] ?? 0);
}
Date: 2022-06-01 - # of leads: 1
Date: 2022-06-02 - # of leads: 0
Date: 2022-06-03 - # of leads: 0
Date: 2022-06-04 - # of leads: 1
Date: 2022-06-05 - # of leads: 0
Date: 2022-06-06 - # of leads: 0
Date: 2022-06-07 - # of leads: 0
Date: 2022-06-08 - # of leads: 0
Date: 2022-06-09 - # of leads: 1
Date: 2022-06-10 - # of leads: 1
Date: 2022-06-11 - # of leads: 0
Date: 2022-06-12 - # of leads: 0
Date: 2022-06-13 - # of leads: 1
Date: 2022-06-14 - # of leads: 2
Date: 2022-06-15 - # of leads: 1
Date: 2022-06-16 - # of leads: 0
Date: 2022-06-17 - # of leads: 0
Date: 2022-06-18 - # of leads: 0
Date: 2022-06-19 - # of leads: 3
Date: 2022-06-20 - # of leads: 0
Date: 2022-06-21 - # of leads: 1
Date: 2022-06-22 - # of leads: 0
Date: 2022-06-23 - # of leads: 0
Date: 2022-06-24 - # of leads: 1
Date: 2022-06-25 - # of leads: 0
Date: 2022-06-26 - # of leads: 0
Date: 2022-06-27 - # of leads: 0
Date: 2022-06-28 - # of leads: 2
Date: 2022-06-29 - # of leads: 1
Date: 2022-06-30 - # of leads: 2