This question already has answers here:
How to pause a script just for a fraction of a second in PHP using the SLEEP() function?
(4 answers)
Closed 3 years ago.
Does PHP provide a function to sleep in milliseconds?
Right now, I'm doing something similar to this, as a workaround.
$ms = 10000;
$seconds = round($ms / 1000, 2);
sleep($seconds);
I'd like to know whether there is a more generic function that is available in PHP to do this, or a better way of handling this.
This is your only pratical alternative: usleep - Delay execution in microseconds
So to sleep for two miliseconds:
usleep( 2 * 1000 );
To sleep for a quater of a second:
usleep( 250000 );
Note that sleep() works with integers, sleep(0.25) would execute as sleep(0) Meaning this function would finish immediatly.
$i = 0;
while( $i < 5000 )
{
sleep(0.25);
echo '.';
$i++;
}
echo 'done';
Related
Here is my pseudo code
For loop repeat 30 times
Call and execute API script which takes less than 1 second
I want the system to sleep for some moment(less than 1 second)
Loop end
I want the above script should finish execution in minimum of 30 seconds (2-3 seconds longer is not a problem but must not less).
Can you write a sample code for me(only time related)
Here's a little program that implements the essence of what I believe you are looking for:
$minTime = 30;
$perSec = 10000;
$start = microtime(TRUE)*$perSec;
print("This must take $minTime seconds.\n");
// Replace this loop with what you actually need done
for ($i=0; $i<10; $i++) { print("Doing stuff #{$i}... "); sleep(1); }
// Check difference in timestamps to calculate remaining time
$remain=($minTime*$perSec-(microtime(TRUE)*$perSec-$start))/$perSec;
var_dump(['remain' => $remain]);
if ($remain > 0) {
print("\n$remain seconds remaining... ");
sleep($remain);
}
print("DONE\n");
This code has been updated to use microtime() instead of time(), which only permitted intervals of integer seconds. On systems that support gettimeofday() we can work with microseconds, instead.
This question already has answers here:
Closed 10 years ago.
For invoicing I want to determine if my total amount has an unnecessary cent. e.g. $5.01 or $5.51. If it detects the cent then I run an if statement to remove the cent: minus 0.01
eg.
Change 30.51 to 0.01 and give a warning message
and also
30.51 to 0.51
and also
30.51 to 1
Both come from a POST, so they are both PHP strings.
but no worries - i got it to work now thanks to dmayo
$pieces = explode(".", $TotalAmt);
echo "<br>p1:".$pieces[1]; // piece2
$p1 = $pieces[1];
$spl1 = str_split($p1);
$TAmttmp = 0;
//echo "<br>sp11:".$spl1; // error convert array to string
$TAmttmp = $spl1[0];
echo "<br>TAmttmp: with useless cent".$TAmttmp;
echo "<br>IT:".$IT;
if ($TAmttmp == 0.01) //so if there is 1 cent subtract the useless cent.
$TAmt = $TAmt - 0.01;
echo "<br>TAmt: with useless cent".$TAmt;
echo "<br>TAmt: without useless cent".$TAmt;
#Gumbo is right, we need to know the logic behind your conversions to provide a precise answer.
You could do an explode:
$var = explode('.',$_POST['val']);
$digits = $var[1]; // this will give you the digits after the decimal point
Now you can do what you need based on your logic.
You could also you use a substr() where you find the decimal point (is it always three places from the right?) and then grab the substring.
Not sure which would be more efficient.
I don't know exactly what you want, but perhaps that helps:
$nr = "30.51";
echo round($nr - floor($nr), 2); // 0.51
echo round(($nr * 10 - floor($nr * 10)) / 10, 2); // 0.01
echo round(($nr * 10 - floor($nr * 10)) * 10); // 1
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP negatives keep adding
I have this code here....
$remaining = 0;
foreach($array as $value=>$row){
$remaining = $remaining + $row['remainingbalance'];
}
What its doing is that it is going through all the remaining balances in the array which are -51.75 and -17.85 with the code above I get -69.60 which is correct. But I am wondering how when its two negatives if they could subtract? Is that possible?
I tried this
$remaining = 0;
foreach($clientArrayInvoice as $value=>$row){
$remaining = $remaining + abs($row['remainingbalance']);
}
but it gives me 69.60 without the negative.
Anyone got any ideas?
my goal is to take -51.75 and -17.85 and come up with -33.90 only when its a negative to do subtract. otherwise add
Whenever you add a negative number, you actually subtract the positive value (and the other way around).
So 0 + (-16) = 0 - 16 = -16.
When you call abs() you calculate something completely different.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is this microtime showing up weird in PHP
I use this code to check performance of the script.
$start_time = microtime(1);
// execution some code
$cpu_time = microtime(1) - $start_time;
the output of echo $cpu_time is something like 3.0994415283203E-6
how to display this to real value like 0.000000003099 on the screen ? Is it 0 seconds and very quick execution, right? :)
Right :)
Try using number_format() function:
echo number_format($cpu_time, 12);
Use number_format.
Like:
number_format($microtime, 15);
Use bcsub
Example
echo bcsub ( microtime ( 1 ), $start_time, 16 );
Is there a PHP equivalent to setting timeouts in JavaScript?
In JavaScript you can execute code after certain time has elapsed using the set time out function.
Would it be possible to do this in PHP?
PHP is single-threaded, and in general PHP is focused on the HTTP request cycle, so this would be tricky to allow a timeout to run code, potentially after the request is done.
I can suggest you look into Gearman as a solution to delegate work to other PHP processes.
You can use the sleep() function:
int sleep ( int $seconds )
// Delays the program execution for the given number of seconds.
Example:
public function sleep(){
sleep(1);
return 'slept for 1 second';
}
This is ugly, but basically works:
<?php
declare(ticks=1);
function setInterval($callback, $ms, $max = 0)
{
$last = microtime(true);
$seconds = $ms / 1000;
register_tick_function(function() use (&$last, $callback, $seconds, $max)
{
static $busy = false;
static $n = 0;
if ($busy) return;
$busy = true;
$now = microtime(true);
while ($now - $last > $seconds)
{
if ($max && $n == $max) break;
++$n;
$last += $seconds;
$callback();
}
$busy = false;
});
}
function setTimeout($callback, $ms)
{
setInterval($callback, $ms, 1);
}
// user code:
setInterval(function() {
echo microtime(true), "\n";
}, 100); // every 10th of a second
while (true) usleep(1);
The interval callback function will only be called after a tickable PHP statement. So if you try to call a function 10 times per second, but you call sleep(10), you'll get 100 executions of your tick function in a batch after the sleep has finished.
Note that there is an additional parameter to setInterval that limits the number of times it is called. setTimeout just calls setInterval with a limit of one.
It would be better if unregister_tick_function was called after it expired, but I'm not sure if that would even be possible unless there was a master tick function that monitored and unregistered them.
I didn't attempt to implement anything like that because this is not how PHP is designed to be used. It's likely that there's a much better way to do whatever it is you want to do.
Without knowing a use-case for your question it's hard to answer it:
If you want to send additional data to the client a bit later you can do a JS timeout on the client side with a handler that will make a new HTTP request to PHP.
If you want to schedule some task for a later time you can store that in a database and poll the DB in regular intervalls. It's not the best peforming solution but relatively easy to implement.
if ($currenturl != $urlto)
exit( wp_redirect( $urlto ) );
You can replace above two line with below code inside your function
if ($currenturl != $urlto)
header( "refresh:10;url=$urlto" );