C.Sing C.Sing - 1 year ago 62
PHP Question

php - How to remove date from an array having a same day of week

I want to remove date from an array by having same day of week from another array.

This was i try:

$intervalDates = ['2017-01-23','2017-01-24','2017-01-25','2017-01-26','2017-01-27','2017-01-28','2017-01-29','2017-01-30','2017-01-31','2017-02-01','2017-02-02','2017-02-03','2017-02-04','2017-02-05','2017-02-06','2017-02-07','2017-02-08','2017-02-09','2017-02-10'];

$tmpWeek = ['2016-10-28','2016-10-29','2016-10-30'];

for($i = 0; $i < count($intervalDates); $i++){

for($j = 0; $j < count($tmpWeek); $j++){

if($intervalDates[$i]->dayOfWeek == $tmpWeek[$j]->dayOfWeek){

$intervalDates = array_values($intervalDates);


but I got the error undefined offset.

Please help.

Answer Source

First create day names array using $tmpWeek, then remove duplicate values from $dayNames. Then loop $intervalDates and remove same days and create new array.

$intervalDates = array('2016-10-28','2016-10-29','2016-10-30','2017-01-23','2017-01-24','2017-01-25','2017-01-26','2017-01-27','2017-01-28','2017-01-29','2017-01-30','2017-01-31','2017-02-01','2017-02-02','2017-02-03','2017-02-04','2017-02-05','2017-02-06','2017-02-07','2017-02-08','2017-02-09','2017-02-10');

$tmpWeek = array('2016-10-28','2016-10-29','2016-10-30');

// create day names array
$dayNames = array();
foreach($tmpWeek as $day){
    $dayNames[] = date('l', strtotime($day));
//  remove duplicate date names
$dayNames = array_unique($dayNames);

$data =array();
foreach($intervalDates as $date){
    if(!in_array(date('l', strtotime($date)), $dayNames)){
            $data[] = $date;

echo "<pre>";
echo "</pre>";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download