$(document).ready(function() {
var t = form.cronometro.value;
var hora = t.substring(0, 2);
var minuto = t.substring(3, 5);
var segundo = t.substring(6, 8);
restore_timer = function() {
send_ajax(form.cronometro.value, "get_time");
}
var interval;
send_ajax = function(time, opt) {
$.ajax({
method: "POST",
async: "false",
url: "timer.php",
data: {"time": time, "opt": opt}
}).done(function(msg) {
if (opt == "get_time") {
if (msg.status == "started") {
$('button').html('Pause');
interval = setInterval('tempo()', 983);
}
form.cronometro.value = msg.time;
t = msg.time;
hora = t.substring(0, 2);
minuto = t.substring(3, 5);
segundo = t.substring(6, 8);
}
});
}
var interval;
timer = function(obj) {
var html = $(obj).html();
if (html == 'Play') {
interval = setInterval('tempo()', 983);
send_ajax("start_timer");
$(obj).html('Pause');
} else {
clearInterval(interval);
send_ajax(form.cronometro.value, "save_time");
$(obj).html('Play');
}
}
$(window).unload(function() {
send_ajax(form.cronometro.value, "save_time");
});
tempo = function(){
if (segundo < 59) {
segundo++;
if (segundo < 10)
segundo = "0" + segundo;
} else if (segundo == 59 && minuto < 59) {
segundo = 0 + "0";
minuto++;
if (minuto < 10)
minuto = "0" + minuto;
}
if (minuto == 59 && segundo == 59) {
segundo = 0 + "0";
minuto = 0 + "0";
hora++;
if (hora < 10)
hora = "0" + hora;
}
form.cronometro.value = hora + ":" + minuto + ":" + segundo;
}
});
if ($_POST) {
$opt = $_POST['opt'];
$servername = "HOST_HERE";"localhost";
$username = "USERNAME_HERE";"u958100481_luis";
$password = "PASSWORD_HERE";"VuH6aSgmNt";
$dbname = "DB_HERE";"u958100481_timer";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($opt == "get_time") {
$sql = "SELECT * FROM timer";time_stamps WHERE id = 1";
$result = $conn->query($sql);
$old_time_stamp;
header('Content-Type: application/json');
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
header$old_time_stamp = $row["time_stamp"];
}
$date = new DateTime('Content);
$time_stamp = $date-Type>getTimestamp();
echo json_encode(array("status" => "started", "time" => date('H:i:s', application/json'($time_stamp - $old_time_stamp))));
} else {
$sql = "SELECT * FROM timer WHERE id = 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo json_encode(array("time" => $row["time"]));
}
}
}
} elseif ($opt == "start_timer") {
$date = new DateTime();
$time_stamp = $date->getTimestamp();
$sql = "INSERT INTO time_stamps VALUES (1, '{$time_stamp}')";
$result = $conn->query($sql);
} elseif ($opt == "save_time") {
$sql = "SELECT * FROM time_stamps WHERE id = 1";
$result = $conn->query($sql);
$old_time_stamp;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$old_time_stamp = $row["time_stamp"];
}
}
$sql = "DELETE FROM time_stamps WHERE id = 1";
$result = $conn->query($sql);
$date = new DateTime();
$time_stamp = $date->getTimestamp();
$new_time = explode(":", date('H:i:s', ($time_stamp - $old_time_stamp)));
$sql = "SELECT * FROM timer WHERE id = 1";
$result = $conn->query($sql);
$old_time;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$old_time = explode(":", $row["time"]);
}
}
$hour = intval($old_time[0] + $new_time[0]);
$minute = intval($old_time[1] + $new_time[1]);
$second = intval($old_time[2] + $new_time[2]);
while ($second >= 60) {
$second = $second - 60;
$minute++;
}
while ($minute >= 60) {
$minute = $minute - 60;
$hour++;
}
if ($second < 10)
$second = "0" . strval($second);
if ($minute < 10)
$minute = "0" . strval($minute);
if ($hour < 10)
$hour = "0" . strval($hour);
$time = $_POST['time'];"{$hour}:{$minute}:{$second}";
$sql = "UPDATE timer setSET time = '{$time}'";' WHERE id = 1";
$result = $conn->query($sql);
}
$conn->close();
}
CREATE TABLE timer (
id INT PRIMARY KEY,
time VARCHAR(10) NOT NULL
);
CREATE TABLE time_stamp (
id INT PRIMARY KEY,
time_stamp VARCHAR(255) NOT NULL
);
INSERT INTO timer values (DEFAULT1, "00:00:00");