0

I have implode code like this :

$date_condition = implode(" AND a.date = ",$date_interval);

That array looks like this :

$date_interval = Array('2017-01-24','2017-01-25','2017-01-26');

This is what I want to expected become an output :

$date_condition = "'2017-01-24' AND a.date = '2017-01-25' AND a.date = '2017-01-26' ";

But from my code that I was try I just get output like this :

$date_condition = "2017-01-24 AND a.date = 2017-01-25 AND a.date = 2017-01-26";

How can I do that ?

Thank you.

Antonio
  • 755
  • 4
  • 14
  • 35

2 Answers2

1

Either change the array:

$date_interval = Array("'2017-01-24'","'2017-01-25'","'2017-01-26'");

Loop through it:

foreach($date_interval as $k => $v){
  $date_interval[$k] = "'$v'";
}

Create a function:

function quotate(array $list){
  foreach($list as $v){
    $ret[] = "'$v'";
  }

  return $ret;
}

$date_condition = implode(" AND a.date = ",quotate($date_interval));

Or do it by adding quotes to this ' AND a.date = ' string, but Suchit beat me to it.

Xorifelse
  • 7,878
  • 1
  • 27
  • 38
1

Try this:

$date_interval = Array('2017-01-24','2017-01-25','2017-01-26');
$date_condition ="'". implode("' AND a.date = '",$date_interval)."'";
echo $date_condition;

DEMO HERE

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44