gpt4 book ai didi

php - substr_replace() 在 WHILE 循环中无法按预期工作

转载 作者:行者123 更新时间:2023-11-30 01:02:48 25 4
gpt4 key购买 nike

substr_replace() 无法按预期工作,因为我处于 while 循环

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

哪里

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

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

我期待$cane

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

可能因为我在“while”中,所以它又添加了一个“(\”

原始字符串

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

最佳答案

据我所知,您来自 $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;

关于php - substr_replace() 在 WHILE 循环中无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19960349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com