English 中文(简体)
PHP开始时间和最终时间问题
原标题:PHP begin time and end time issue
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;  
    }
}

Now when i echo in end_process function . Suppose end_time is 1318412390 i get begin_time same as end_time. Why does this happen.. How do get the original begin_time which was set in first function

最佳回答

Try using microtime。 它对执行时间少于第二次的情况给予更准确的时间解读。

<><>Edit>/strong>

<代码>microtime(true);将时间作为浮动时间回来,以便更容易看到差异。

问题回答

时间(a);给你写信,但你实际处理的情况只是其中的一小部分。 正如间谍指出的,你希望用微观时间来做!

<?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);

我进行了快速测试:

<?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

Please post your code.

这部法律是:

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

You ll see that is works, so that piece of code isn t the problem. The error must be in some other part of the code.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签