-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimecheck.php
More file actions
71 lines (56 loc) · 1.79 KB
/
Copy pathtimecheck.php
File metadata and controls
71 lines (56 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
if($_POST['action']=="startOrStopTimer")
startOrStopTimer($_POST);
if($_POST['action']=="checkTimer")
checkTimer();
function connect(){
try{
if($conn = new PDO('mysql:host=localhost;dbname=synced_watch','usr','pw')){
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
return $conn;
}
else{
return "fuck";
}
} catch(PDOException $e) {
return 'Connection failed: ' . $e->getMessage();
}
}
function checkTimer(){
try{
$conn = connect();
$stmt = $conn->prepare("SELECT CURRENT_TIMESTAMP as curtime, UNIX_TIMESTAMP(CURRENT_TIMESTAMP) as unixtime, started, start_time from timer_on where id=1");
$stmt->execute(array());
$ret = $stmt->fetchAll(PDO::FETCH_ASSOC)[0];
echo json_encode($ret);
} catch(PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
function startOrStopTimer($timer_arr){
$starting = $timer_arr['start'];
try{
$conn = connect();
if($starting=="0")
$start_time = ", start_time=0 ";
else{
//if hour minute and second is 0 then go on like normal.
if($timer_arr['hour']=="0" && $timer_arr['minute']=="0" && $timer_arr['second']=="0")
$start_time = ", start_time=UNIX_TIMESTAMP(CURRENT_TIMESTAMP) ";
else{
//calculate x back from starttime where x is unixtimestamp of now - total minutes
$hour_to_unix = (int)$timer_arr['hour'] * 60 * 60;
$minute_to_unix = (int)$timer_arr['minute'] * 60;
$to_sub = $hour_to_unix + $minute_to_unix + (int)$timer_arr['second'];
$start_time = ", start_time=UNIX_TIMESTAMP(CURRENT_TIMESTAMP)-".$to_sub." ";
}
}
$stmt = $conn->prepare("UPDATE timer_on SET started=".$starting.$start_time." where id=1");
$stmt->execute(array());
echo $stmt->rowCount();
}
catch(PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
?>