2016-11-18 51 views
1

嗨,大家好我有一個開始,在特定的時間結束的代碼,我的代碼是做工精細,但我想在數據使用此代碼迴路 例如在循環,我會煲名我想爲每一個開始和結束的具體時間爲:代碼開始在特定的時間內結束

這是我的代碼:

<?php 
date_default_timezone_set('America/New_York'); 
$time = date('Y:m:d H:i:s'); 
$timestart = date('Y:m:d H:i:s'); //time start 
$timeend = '2016:11:17 10:56:00'; //time end 

if($time >= $timeend){ 
echo "time end"; 
}else{ 
    echo 'untel end time'; 
    } 


$now = new DateTime(); 
$future_date = new DateTime($timeend); 
$interval = $future_date->diff($now); 
?> 

,我想知道如何與循環數據使用它呢?

謝謝。

+0

您需要cron才能在設定的時間運行腳本 – Blinkydamo

+0

不需要cron – AllordEmad

+0

那麼您是否正在連續循環中運行腳本以檢查時間嗎? – Blinkydamo

回答

1

您可以使用EV或事件擴展或循環實現。我寧願EV延長這項任務

再舉例來說,創建定時器:

// Required create variable! 
$w = new EvTimer($needWorkedSeconds, $repeatAfterSecond, function ($w) { 
    echo "iteration = ", Ev::iteration(), PHP_EOL; 
}); 

// Loop until Ev::stop() is called or all of watchers stop 
Ev::run(); 

More read here!

或使用事件(不過我更喜歡事件與插座工作):

$base = new \EventBase(); 
$e = \Event::timer($base, function($n) use (&$e) { 
    echo "$n seconds elapsed\n"; 
    if($isTimeEndNow) 
    { 
     $e->delTimer(); 
    } 
}, $repeatAfterSecond); 
$e->addTimer($repeatAfterSecond); 
$base->loop(); 

More read here!

或者你可以嘗試得到控制而即,例如:

while(true) 
{ 
    if($isTimeEndNow) 
    { 
     break; 
    } 
    sleep($repeatAfterSecond); 
} 

與實施例I使用undeclareted變量:

$repeatAfterSecond - seconds to next iteration or next call 
$isTimeEndNow - this is: time() > $endTimestamp 
$needWorkedSeconds - this is seconds: time_start - time_end 

注意瞬!!!小心!我想你犯錯,如果你想使用MySQL,如果你需要在具體時間死腳本。檢查你的算法!

相關問題