class Test extends Controller {
public function start_process(){
$begin_time = time();
echo $begin_time ; // suppose it shows 1318412333
// statement 1
// statement 2
// statement 3
// statement 4
// statement 5
// statement 6
// some more code
$this->end_process($begin_time);
}
public function end_process($begin_time){
$end_time = time();
echo $begin_time . " " . $end_time;
}
}
现在我在end_process函数中回显。假设end_time是1318412390,我得到的begin_time与end_time相同。为什么会发生这种情况..如何获得在第一个函数中设置的原始begin_time
答案 0 :(得分:3)
答案 1 :(得分:0)
时间();给你秒,但你实际处理的是它的分数。蜘蛛指出,你想要使用microtime()!
<?php
$start = microtime(true);
// -- do something --
// calulcate duration between $start and now
$duration = microtime(true) - $start;
// print in the format 1.2345 for better reading
printf("took %0.4d seconds", $duration);
答案 2 :(得分:0)
我做了一个快速测试:
<?php
ini_set('display_errors', true);
class test {
public function start_process(){
$begin_time = time();
echo $begin_time . "<br>"; // suppose it shows 1318412333
// statement 1
// statement 2
// statement 3
// statement 4
// statement 5
// statement 6
// some more code
sleep(1);
$this->end_process($begin_time);
}
public function end_process($begin_time){
$end_time = time();
echo $begin_time . " <br> " . $end_time;
}
}
$test = new test();
$test->start_process();
?>
我的结果是
1318415493
1318415493
1318415494
请发布您的代码。
答案 3 :(得分:0)
只需运行此代码:
$test = new Test();
$test->start_process();
class Test {
public function start_process(){
$begin_time = microtime(true);
echo $begin_time . ' '; // suppose it shows 1318412333
for($i=0;$i<10000;$i++) {
//just some delay as if the script is doing something
}
$this->end_process($begin_time);
}
public function end_process($begin_time){
$end_time = microtime(true);
echo $begin_time . " " . $end_time;
}
}
你会发现这是有效的,所以这段代码不是问题。错误必须在代码的其他部分。