I have the store start time as microtime() and end time as microtime() into database.
Now I want to calculate how long the script takes the seconds/minutes/hours to execute.
How can I do in PHP?
asked Jun 24, 2011 at 12:59
0
basically, like this:
echo date("H:i:s",$endtime-$starttime);$endtime-$starttime gives the duration in seconds, date is used to format the output. sounds like saving to and reading from a database isn't your problem here, so i left that out in my example. Note that you'll have to use microtime(true) to get this working, with the space-separated output of microtime() your can't do calculations that easy.
EDIT: you could do all the calculation on your own, too. it's just basic math like this:
$duration = $endtime-$starttime; $hours = (int)($duration/60/60); $minutes = (int)($duration/60)-$hours*60; $seconds = (int)$duration-$hours*60*60-$minutes*60;
danronmoon
3,7045 gold badges33 silver badges56 bronze badges
answered Jun 24, 2011 at 13:03
oezioezi
50k10 gold badges97 silver badges115 bronze badges
5
microtime to seconds or hours or min conversion?
microtime is the name of the php function that return a measure of time in microseconds and basically microseconds can be converted to:
1 milliseconds = 1,000 microseconds 1 second = 1,000,000 microseconds 1 minute = 60,000,000 microseconds 1 hour = 3,600,000,000 microseconds or 1 microsecond = 0.001 milliseconds 1 microsecond = 0.000001 seconds 1 microsecond = 0.0000000166666667 minutes 1 microsecond = 0.000000000277777778 hours
answered Sep 6, 2013 at 12:33
user2754369user2754369
2212 silver badges2 bronze badges
function formatPeriod($endtime, $starttime) { $duration = $endtime - $starttime; $hours = (int) ($duration / 60 / 60); $minutes = (int) ($duration / 60) - $hours * 60; $seconds = (int) $duration - $hours * 60 * 60 - $minutes * 60; return ($hours == 0 ? "00":$hours) . ":" . ($minutes == 0 ? "00":($minutes < 10? "0".$minutes:$minutes)) . ":" . ($seconds == 0 ? "00":($seconds < 10? "0".$seconds:$seconds)); }
Giacomo1968
25.2k11 gold badges69 silver badges97 bronze badges
answered Dec 14, 2015 at 9:54
ahmed redaahmed reda
1532 silver badges5 bronze badges
If you have a time A and a time B, in seconds, then the number of seconds between those two absolute times is:
B - AIf you want to format this number of seconds, you can use date to prettify it.
answered Jun 24, 2011 at 13:04
Definition and Usage
The microtime() function returns the current Unix timestamp with microseconds. By default, this function returns a string value which contains microseconds and seconds separated by space (msec sec).
Syntax
microtime($get_as_float)Parameters
1 | get_as_float(Optional) This is a boolean value which is used to specify whether the result should be a floating point value or not. If you pass the boolean value true as a parameter, this function returns result as floating point value. |
Return Values
PHP microtime() function returns the current Unix timestamp. By default this returns a string value in the form msec sec. If you pass the boolean value true as a parameter to this method, it returns the current time in seconds since the Unix epoch accurate to the nearest microsecond.
PHP Version
This function was first introduced in PHP Version 4 and, works with all the later versions.
Example
Following example demonstrates the usage of the microtime() function −
Live Demo
<?php $time = microtime(); print($time); ?>This will produce following result −
0.60664200 1589305212Example
Lets try to set the get_as_float value to true −
Live Demo
<?php $time = gettimeofday(true); print_r($time); ?>This will produce following result −
1589298812.5101Example
Live Demo
<?php $time_start = microtime(true); usleep(100); $time_end = microtime(true); $time = $time_end - $time_start; echo "Did nothing in $time seconds\n"; ?>This produces the following result −
Did nothing in 0.0018141269683838 secondsphp_function_reference.htm