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

时间:2014-11-15 12:04:14

标签: php

我需要编写一个简单的PHP代码,它会逐日减少,并在特定日期命中时停止(在本例中为昨天)。我写了下面的代码片来做这个,但这似乎是永远循环...没有弯腰在我设置的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));
}

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

$time >= ($time - (24 * 3600))总是如此。这与说

相同
n >= n - 1

对所有n来说都是显而易见的。

这样写:

$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));
}

答案 1 :(得分:1)

我认为问题是你在for循环中使用了time()函数。尝试在循环之前设置开始和结束变量。

//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));
}