0

substr_replace() doens't work as expected because i'm in while loop

PHP

$c= "2013-01-01 12:00:00";
$d= "2013-01-02 12:00:00";
$date_3 = date("Y-m-d g:i:s", strtotime("$c"));
$date_4 = date("Y-m-d g:i:s", strtotime("$d"));

$results = array($date_1);
$i = $date_3;
while ($i <= $date_4) {//here start the while look
$i = date("Y-m-d g:i:s", strtotime($i));
array_push($results, $i);
$k= $i . "\n";
$chunks = str_split($k, 19);
$nexstring = join('\')', $chunks);//2013-01-01 12:00:00') 2013-01-02 12:00:00') 2013-01-03 12:00:00')
$cane = implode(
', (\'',
str_split($nexstring, 21)//2013-01-01 12:00:00'), (' 2013-01-02 12:00:00'), (' 
);
echo substr_replace($cane, '(\'', 0, 0);//sub_string doesn't give my expected output
$i = date("Y-m-d g:i:s",strtotime("+1 day", strtotime($i)));
}//end while

where

 $nexstring = 2013-01-01 12:00:00'), (' 2013-01-02 12:00:00'), (' 

and

 $cane=('2013-01-01 12:00:00'), (' ('2013-01-02 12:00:00'), ('//1 more parenthesis added to the second date.

I'm expecting $cane to be

('2013-01-01 12:00:00'), (' 2013-01-02 12:00:00'), ('// my expected output

Probably as I'm in the "while" it adds one more '(\'' of those

Original string

2013-01-01 12:00:00 2013-01-02 12:00:00 
4

1 回答 1

0

据我所见,您来自$date_1$date_4- 不清楚 - 并希望将 2 天添加为另外两个日期,并将其格式化为组合字符串('2013-01-01 12:00:00'), ('2013-01-02 12:00:00'), ('2013-01-03 12:00:00')- 同一时间,仅一两天后。那这个呢:

$date = new DateTime($date_1);
$out = "('" . $date->format('Y-m-d H:i:s') . "')";
for ( $n = 1; $n < 3; $n ++ )
{
   $date->add(new DateInterval('P1D'));
   $out .= ", ('" . $date->format('Y-m-d H:i:s') . "')";
}
echo $out;
于 2013-11-13T18:02:15.413 回答