简体   繁体   English

PHP Simple For循环仅使用时间函数循环今天的日期

[英]PHP Simple For loop only loop today's date with time function

I need to write a simple php code that decrements day by day and stop when a certain date hits (in this case yesterday). 我需要编写一个简单的php代码,该代码逐日递减,并在达到某个日期时停止(在本例中为昨天)。 I have written following code piece to do this but this seems to be looping forever ... not stooping at the forloop break condition i set 我写了下面的代码来做到这一点,但这似乎永远循环...不弯腰我设置的forloop中断条件

//start time to decrements
$time = time(); // for now start with today or set with strtotime('2010-08-01 00:00:00');
for ($time = time(); $time >= ($time - (24 * 3600)); $time = ($time - (24 * 3600))) {
    run("www.xyx.com",date("Y/m/d", $time));
}

Any ideas ? 有任何想法吗 ?

$time >= ($time - (24 * 3600)) is always going to be true. $time >= ($time - (24 * 3600))总是正确的。 It is the same as saying 就像说一样

n >= n - 1

which is self-evidently true for all n. 不言而喻,这对所有n都是如此。

Write it like this instead: 改为这样写:

$end_time = time() - (24 * 3600);    // yesterday
for ($time = time(); $time >= $end_time; $time = ($time - (24 * 3600))) {
    run("www.xyx.com",date("Y/m/d", $time));
}

I think the problem is your use of the time() function from within the for loop. 我认为问题是您在for循环中使用了time()函数。 Try setting the start and stop variables before the loop. 尝试在循环之前设置开始和停止变量。

//start time to decrements
$starttime = time(); // for now start with today or set with strtotime('2010-08-01 00:00:00');
$stoptime = $starttime - (24*3600);
for ($time = $starttime; $time >= $stoptime; $time = ($time - (24 * 3600))) {
    //echo date("Y/m/d", $time) . "\n";
    run("www.xyx.com",date("Y/m/d", $time));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM