Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 意外的while循环行为_Php_While Loop_Strtotime - Fatal编程技术网

Php 意外的while循环行为

Php 意外的while循环行为,php,while-loop,strtotime,Php,While Loop,Strtotime,我尝试过以不同的方式和不同的位置构建和放置while循环,并将$val视为放置在while循环中的正确/错误变量,但我就是不明白。我预计产量为:815 830 845 900。。。。。。。直到1900年 相反,我得到815,然后是1915,1930,1945,一直到0000,然后从1915开始循环。有人请告诉我我把什么放错了位置,或者我用错了什么变量 date_default_timezone_set('America/New_York'); $st='800'; $et='1900'; $fr

我尝试过以不同的方式和不同的位置构建和放置while循环,并将$val视为放置在while循环中的正确/错误变量,但我就是不明白。我预计产量为:815 830 845 900。。。。。。。直到1900年

相反,我得到815,然后是1915,1930,1945,一直到0000,然后从1915开始循环。有人请告诉我我把什么放错了位置,或者我用错了什么变量

date_default_timezone_set('America/New_York');
$st='800';
$et='1900';
$frac = 900;
$x = $et;
$val=0;

if (strlen($st) < 4) {$st = "0".$st;}
$current_time = strtotime(date($st));
$r = $current_time % $frac;
$new_time = $current_time + ($frac-$r);
$val = date('Gi', $new_time);
echo $val."<br>";

while ($val !== $et){
if (strlen($val) < 4) {$st = "0".$val;}
$current_time = strtotime('+ 15 minutes',date($val));
$r = $current_time % $frac;
$new_time = $current_time + ($frac-$r);
$val = date('Gi', $new_time);
echo $val."<br>";
}
date\u default\u timezone\u set(“美国/纽约”);
$st='800';
$et='1900';
$frac=900;
$x=$et;
$val=0;
if(strlen($st)<4{$st=“0”。$st;}
$current_time=strottime(日期($st));
$r=$current_time%$frac;
$new_time=$current_time+($frac-$r);
$val=日期('Gi',$new_时间);
echo$val.“
”; 而($val!=$et){ if(strlen($val)<4{$st=“0”。$val;} $current_time=strottime('+15分钟',date($val)); $r=$current_time%$frac; $new_time=$current_time+($frac-$r); $val=日期('Gi',$new_时间); echo$val.“
”; }

给降价玩家的提示——编辑的帖子回答:你可以随心所欲地抨击这个问题,但事实是,这是一个明确的问题,明确的目标试图达成,明确的尝试(其中一个)正确编码失败了,所以编码不好,努力不坏。所以最终我请求帮助。你使用降价系统的方式和理由都是错误的,但不管怎样,我都不指望你会在意。感谢那些做出了实际建设性努力并提供了指导/帮助的人。

对于不合法的for loop:

for ($i = 800, $t= 1900; $i <= $t; $i += 15) { 
    printf('%1$3d <br>', $i); 
    if ($i%100 >= 45) $i += 40; 
}
对于($i=800,$t=1900;$i=45)$i+=40;
}
带日期周期:

$timezone = new DateTimeZone("America/New_York");
$begin = new DateTime("now", $timezone);
$begin->setTime(8, 0, 0);
$end = new DateTime("now", $timezone);
$end->setTime(19, 0, 0);

$interval = new DateInterval('PT15M');
$end->add($interval);

$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
    echo $date->format("Gi") . "<br>";
}
$timezone=新日期时区(“美国/纽约”);
$begin=新日期时间(“现在”,“时区”);
$begin->setTime(8,0,0);
$end=新日期时间(“现在”,“时区”);
$end->setTime(19,0,0);
$interval=新的日期间隔('PT15M');
$end->add($interval);
$daterange=新的日期周期($begin,$interval,$end);
foreach($daterange作为$date){
echo$date->format(“Gi”)。“
”; }
发电机:

function xmins($start, $end, $step = 15) {
    if ($start < $end) {
        if (($step <= 0) || ($step >= 40)) {
            throw new LogicException('Step must be in range 1-40');
        }

        for ($i = $start; $i <= $end; $i += $step) {
            if (60 <= $temp = $i % 100) {
                $i += 40;
            }
            yield $i;
            if ((60-$step) === $temp = $i % 100) {
                $i += 100 - $temp - $step;
            }
        }
    }
}

foreach (xmins(800,1900,15) as $i) {
    printf('%1$3d<br>', $i);
}
函数xmins($start,$end,$step=15){
如果($start<$end){
如果($step=40)){
抛出新的LogicException('步骤必须在1-40范围内');
}
对于($i=$start;$i使用日期间隔:

$date = new DateTime('2015-01-01 08:00:00');
$mins = new DateInterval('PT15M');
do {
    $date->add($mins);
    echo $date->format('Hi')."\n";
} while($date->format('Hi') < 1900);
$date=新的日期时间('2015-01-01 08:00:00');
$mins=新日期间隔('PT15M');
做{
$date->add($min);
echo$date->format('Hi')。“\n”;
}而($date->format('Hi')<1900);

你想要从何时到1900的15分钟间隔内的时间吗?我会避免所有的日期/时间数学,只对整数进行循环。当你得到结果时,然后转换为日期/时间。或者找到开始时间和结束时间之间的秒数,并对其进行计算。@dagon是的,我正在尝试更新每个循环b的$val值y以15分钟为增量。起始值为$st,结束值为$et,两者都可能更改,但在本例中,我只设置了values@Dagon我想,“$current_time=strottime(“+15分钟”,date($val))'在while循环中可以这样做,但不是这样。800是从脚本早期部分的$_POST传入/传递的字符串值谢谢,为了接受$st和$et字符串,我不得不稍微修改它以包含以下内容,但无论如何谢谢。我的方法是我猜得太离谱了。
if(strlen($st)<4){$st=“0”。$st;}如果(strlen($et)<4{$st=“0”。$st;}$begin->setTime(substr($st,0,2),substr($st,-2),0);$end->setTime(substr($et,0,2),substr($et,-2),0);
@DMSJax,DateTime类在处理这些东西时真的很有帮助,而且我肯定会好几次派上用场。:)