Start and stop a timer PHP
You can use microtime
and calculate the difference:
$time_pre = microtime(true);
exec(...);
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;
Here's the PHP docs for microtime
: http://php.net/manual/en/function.microtime.php
How to make Stopwatch in php
Use hrtime() https://www.php.net/manual/en/function.hrtime.php
Here is a snippet:
$start=hrtime(true);
sleep(5);//this is what you are going to be measuring
$end=hrtime(true);
$eta=$end-$start;
echo $eta/1e+6; //nanoseconds to milliseconds
//5000.362419
Php timer does not stop at 00:00
We cannot submit a form in AJAX file. Try this.
//timer function
setInterval (function(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET","timer.php",false);
xmlhttp.send(null);
if (xhttp.response != "SUBMIT")
document.getElementById("time_left").innerHTML = xmlhttp.responseText;
} else {
document.getElementById("quiz_form").submit();
}
}, 1000);
//timer.php code
if ($timer_time == "00:00") {
echo "SUBMIT";
} else {
echo "OKAY";
}
How to make incremented timer in Core PHP with start pause resume stop button with mysql
You will need two buttons. One start
another stop
.
When clicking start
you make Ajax request to server (since you labeled question as javascript
).
You have table in database timer
id | user_id | state | time
Ajax request do query to database:
UPDATE timer SET state = 1 WHERE user_id = :user
If there is no such entry for current user - add one first:
"INSERT INTO timer (user_id, state, time) VALUES ({$userId}, ".User::STATUS_INACTIVE.", '".time()."')
Your Cron job executes query to database every 1 second:
UPDATE timer SET time = time + 1
When user clicks stop
you make another request that updates database and returns current time:
UPDATE timer SET state = 0 WHERE user_id = :user;
SELECT time FROM timer WHERE user_id = :user;
As you can see there is no way to execute user commands without javascript.
It would be possible if your user would use CLI.
Php Timer with a Pause Function
I don't think "Javascript timer wouldn't be very accurate" is an accurate statement.
However, if you decide to use PHP, you will not be having the script running for the whole duration of the project. What you need is simply a number of markers in an object. You can store the object in a database table or as JSON string in a file. Here's an example using JSON:
[
[1440264185, "start"],
[1440280217, "pause"],
[1440288349, "resume"],
[1440292161, "pause"],
[1440317465, "resume"],
[1440325597, "stop"]
]
The object is built progressively with each event adding the timestamp of the signal received with the signal type (start
, pause
, resume
, stop
).
Then, when you want to calculate the amount of time spent working on the project/task you can use code like this:
$markers = json_decode($json_markers, true);
$num_markers = count($markers);
$markers[] = $markers[$num_markers-1];
$time_worked = 0;
for($i = 0; $i < $num_markers; $i++) {
if(in_array($markers[$i][1], array("start","resume"))) {
$time_worked += $markers[$i+1][0] - $markers[$i][0];
}
}
var_dump($time_worked); // int(27976)
Here's the code above.
Start and terminate a background python script with PHP and a timer
That's actually pretty simple. You can use a memory object caching system. I would recommend memcached
. Memory objects from memcached
can be accessed literally from anywhere in your system. The only requirement is that a connection to the memcached backend server is supported. (PHP does, Python does, etc.)
Answer to your first question:
Create a variable called stopme
with the value 0
in the memcached
database.
Connect from your python script to the memcached database and read the variable stopme
permanently. Let's say the python script is running when the variable stopme
has the value 0
.
In order to stop your script from PHP, make a connection from your PHP script to the memcached server and set stopme
to 1.
The python script receives the updated value instantly and exits.
Answer to your second question:
It could be done like explained in my answer before through reading shared variables, but additionally I would like to mention that you also could use a cronjob to kill a running script.
How to get this timer to Only start upon form submission, then to execute php code 1st. then disable submit button, wait 30, then re-enable?
Set the button as disable by default. If you need to make it available the first time, then check if you have the button submit using $_POST
values:
<?php $disable = isset($_POST['link_to_php']) ? '':'disabled'; ?>
<input type="submit" name="link_to_php" value="Attempt" id="myButton" <?=$disabled?>>
Enable when counter is 0:
if (counter <= 0) {
....
document.getElementById('myButton').disabled = false;
Javascript and PHP countdown Timer that displays the same for everyone
I have finally come up with a solution that works. There are still elements I haven't quite figured out how to fix, but the following code essentially does exactly what I need for now.
setInterval(function() {
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
var x = document.getElementById("timer");
var d = new Date();
var s = (d.getSeconds());
var m = (d.getMinutes());
var a = addZero(30 - m);
var b = addZero(60 - m);
var c = (60 - s);
var z = "<span style='color:red;font-size:50px;'>" + "Break" + "</span>";
var v = "<span style='color:black;font-size:24px;'>" + "Break" + "</span>";
if (m > 30) {
y = b;
}
else if (m < 30) {
y = a;
}
if (y < 2 && c < 15) {
q = z;
}
else {
q = v;
}
var t = y + (":" + addZero(c) + " Till Station " + (q));
x.innerHTML = t;
}, 250);
<div align="center" id="timer" style='color:black;font-size:24px;' ></div>
Related Topics
Get the First or Last Friday in a Month
Compile Error: Cannot Use Isset() on the Result of an Expression
Proper Prevention of Mail Injection in PHP
PHP to Clean-Up Pasted Microsoft Input
Destroy Session When Broswer Tab Closed
Special Characters in "File_Exists" Problem (Php)
How to Change the Woocommerce_Form_Field HTML Structure
Long Integer Is Transformed When Inserted in Shorter Column, Not Truncated. Why? What Is the Formula
How to Put JavaScript Variable in PHP Echo
Return Multiple Response Data in One Response
Using Value of a Column as Index in Results Using Pdo
Get Coupon Data from Woocommerce Orders
How to Create an Empty Array in PHP with Predefined Size
Laravel Escaping All HTML in Blade Template
PHP Exec - Check If Enabled or Disabled
Backslash in PHP -- What Does It Mean
PHP Post_Max_Size VS Upload_Max_Filesize, What Is the Difference