Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function calculateAlarmTime($addMinutes) {
- // Calculate the future timestamp by adding the specified number of minutes to the current timestamp
- $futureTime = time() + ($addMinutes * 60);
- return $futureTime;
- }
- function insertDataAlarm($userid, $panenTime, $comment) {
- // SQLite database file path
- $dbPath = 'alarm.db';
- $status = 0;
- // Connect to SQLite database
- $db = new SQLite3($dbPath);
- if (!$db) {
- die("Error connecting to the database.");
- }
- // Prepare the SQL statement
- $stmt = $db->prepare("INSERT INTO your_table_name (userid, panenTime, status, comment) VALUES (:userid, :panenTime, :status, :comment)");
- // Bind parameters
- $stmt->bindParam(':userid', $userid, SQLITE3_INTEGER);
- $stmt->bindParam(':panenTime', $panenTime, SQLITE3_INTEGER);
- $stmt->bindParam(':status', $status, SQLITE3_TEXT);
- $stmt->bindParam(':comment', $comment, SQLITE3_TEXT);
- // Execute the statement
- $result = $stmt->execute();
- // Check for success
- if ($result) {
- return "Alarm berhasil ditambah.";
- } else {
- return "Error inserting data: " . $db->lastErrorMsg();
- }
- // Close the database connection
- $db->close();
- }
- function createDatabase() {
- // SQLite database file path
- $dbPath = 'alarm.db';
- // Connect to SQLite database (or create it if not exists)
- $db = new SQLite3($dbPath);
- if (!$db) {
- die("Error connecting to the database.");
- }
- // Create the table if not exists
- $query = "CREATE TABLE IF NOT EXISTS your_table_name (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- userid INTEGER,
- panenTime INTEGER,
- status TEXT,
- comment TEXT
- )";
- $db->exec($query);
- // Check for success
- if ($db->lastErrorCode() == 0) {
- echo "Database and table created successfully.";
- } else {
- echo "Error creating database or table: " . $db->lastErrorMsg();
- }
- // Close the database connection
- $db->close();
- }
- function checkAndUpdateStatus() {
- // SQLite database file path
- $dbPath = 'alarm.db';
- // Connect to SQLite database
- $db = new SQLite3($dbPath);
- if (!$db) {
- die("Error connecting to the database.");
- }
- // Get the current Unix timestamp
- $currentTimestamp = time();
- // Prepare the SQL statement to fetch records where timestamp is less than current time and status is 0
- $stmt = $db->prepare("SELECT * FROM your_table_name WHERE panenTime < :currentTimestamp AND status = 0");
- // Bind parameters
- $stmt->bindParam(':currentTimestamp', $currentTimestamp, SQLITE3_INTEGER);
- // Execute the statement
- $result = $stmt->execute();
- // Check for success
- if ($result) {
- // Fetch the records
- while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
- // Update the status or perform any other action
- $id = $row['id'];
- $userid = $row['userid'];
- $panenTime = date('H:i:s d-m-Y', $row['panenTime']);
- echo "Updating status for record with ID $id\n";
- echo "userid: " . $userid . "\n";
- echo "panenTime: " . $panenTime . "\n";
- // Example: Update the status to 1
- $updateStmt = $db->prepare("UPDATE your_table_name SET status = 1 WHERE id = :id");
- $updateStmt->bindParam(':id', $id, SQLITE3_INTEGER);
- $updateStmt->execute();
- }
- } else {
- echo "Error querying database: " . $db->lastErrorMsg();
- }
- // Close the database connection
- $db->close();
- }
- function AlarmFarm($userid, $panenTime, $comment) {
- insertDataAlarm($userid, calculateAlarmTime($panenTime), $comment);
- }
Add Comment
Please, Sign In to add comment