xpppppppaicyber

alarm.php

Dec 21st, 2023 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.77 KB | None | 0 0
  1. <?php
  2.  
  3. function calculateAlarmTime($addMinutes) {
  4.     // Calculate the future timestamp by adding the specified number of minutes to the current timestamp
  5.     $futureTime = time() + ($addMinutes * 60);
  6.  
  7.     return $futureTime;
  8. }
  9.  
  10. function insertDataAlarm($userid, $panenTime, $comment) {
  11.     // SQLite database file path
  12.     $dbPath = 'alarm.db';
  13.     $status = 0;
  14.     // Connect to SQLite database
  15.     $db = new SQLite3($dbPath);
  16.  
  17.     if (!$db) {
  18.         die("Error connecting to the database.");
  19.     }
  20.  
  21.     // Prepare the SQL statement
  22.     $stmt = $db->prepare("INSERT INTO your_table_name (userid, panenTime, status, comment) VALUES (:userid, :panenTime, :status, :comment)");
  23.  
  24.     // Bind parameters
  25.     $stmt->bindParam(':userid', $userid, SQLITE3_INTEGER);
  26.     $stmt->bindParam(':panenTime', $panenTime, SQLITE3_INTEGER);
  27.     $stmt->bindParam(':status', $status, SQLITE3_TEXT);
  28.     $stmt->bindParam(':comment', $comment, SQLITE3_TEXT);
  29.  
  30.     // Execute the statement
  31.     $result = $stmt->execute();
  32.  
  33.     // Check for success
  34.     if ($result) {
  35.         return "Alarm berhasil ditambah.";
  36.     } else {
  37.         return "Error inserting data: " . $db->lastErrorMsg();
  38.     }
  39.  
  40.     // Close the database connection
  41.     $db->close();
  42. }
  43.  
  44.  
  45. function createDatabase() {
  46.     // SQLite database file path
  47.     $dbPath = 'alarm.db';
  48.  
  49.     // Connect to SQLite database (or create it if not exists)
  50.     $db = new SQLite3($dbPath);
  51.  
  52.     if (!$db) {
  53.         die("Error connecting to the database.");
  54.     }
  55.  
  56.     // Create the table if not exists
  57.     $query = "CREATE TABLE IF NOT EXISTS your_table_name (
  58.        id INTEGER PRIMARY KEY AUTOINCREMENT,
  59.        userid INTEGER,
  60.        panenTime INTEGER,
  61.        status TEXT,
  62.        comment TEXT
  63.    )";
  64.  
  65.     $db->exec($query);
  66.  
  67.     // Check for success
  68.     if ($db->lastErrorCode() == 0) {
  69.         echo "Database and table created successfully.";
  70.     } else {
  71.         echo "Error creating database or table: " . $db->lastErrorMsg();
  72.     }
  73.  
  74.     // Close the database connection
  75.     $db->close();
  76. }
  77.  
  78. function checkAndUpdateStatus() {
  79.     // SQLite database file path
  80.     $dbPath = 'alarm.db';
  81.  
  82.     // Connect to SQLite database
  83.     $db = new SQLite3($dbPath);
  84.  
  85.     if (!$db) {
  86.         die("Error connecting to the database.");
  87.     }
  88.  
  89.     // Get the current Unix timestamp
  90.     $currentTimestamp = time();
  91.  
  92.     // Prepare the SQL statement to fetch records where timestamp is less than current time and status is 0
  93.     $stmt = $db->prepare("SELECT * FROM your_table_name WHERE panenTime < :currentTimestamp AND status = 0");
  94.  
  95.     // Bind parameters
  96.     $stmt->bindParam(':currentTimestamp', $currentTimestamp, SQLITE3_INTEGER);
  97.  
  98.     // Execute the statement
  99.     $result = $stmt->execute();
  100.  
  101.     // Check for success
  102.     if ($result) {
  103.         // Fetch the records
  104.         while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
  105.             // Update the status or perform any other action
  106.             $id = $row['id'];
  107.             $userid = $row['userid'];
  108.             $panenTime = date('H:i:s d-m-Y', $row['panenTime']);
  109.             echo "Updating status for record with ID $id\n";
  110.             echo "userid: " . $userid . "\n";
  111.             echo "panenTime: " . $panenTime . "\n";
  112.  
  113.             // Example: Update the status to 1
  114.             $updateStmt = $db->prepare("UPDATE your_table_name SET status = 1 WHERE id = :id");
  115.             $updateStmt->bindParam(':id', $id, SQLITE3_INTEGER);
  116.             $updateStmt->execute();
  117.         }
  118.     } else {
  119.         echo "Error querying database: " . $db->lastErrorMsg();
  120.     }
  121.  
  122.     // Close the database connection
  123.     $db->close();
  124. }
  125.  
  126. function AlarmFarm($userid, $panenTime, $comment) {
  127.     insertDataAlarm($userid, calculateAlarmTime($panenTime), $comment);
  128. }
  129.  
  130.  
  131.  
  132.  
Add Comment
Please, Sign In to add comment