<?php

require_once './connection.php';

function metersToFeet($meters) {
    $feet = $meters * 3.28084; // Convert meters to feet
    return $feet;
}

$user_id = 4;
//                                                                                print_r($result);
$query1 = 'SELECT tfmd.*, tfm.meter_name, tfm.address, DATE_FORMAT(tfmd.created_date, "%d-%m-%Y %H:%i:%s") AS created_date_new, DATE_FORMAT(tfmd.created_date, "%Y-%m-%d") AS days_date FROM tbl_flow_meter_data tfmd JOIN ( SELECT flow_meter_id, MAX(id) AS max_id FROM tbl_flow_meter_data WHERE flag = 1 GROUP BY flow_meter_id ) latest ON tfmd.id = latest.max_id LEFT JOIN tbl_flow_meter tfm ON tfm.id = tfmd.flow_meter_id WHERE tfm.flag = 1';
//                                                                                $query1 = 'SELECT tfmd.*,DATE_FORMAT(tfmd.created_date,"%d-%m-%Y %H:%i:%s") AS created_date_new,DATE_FORMAT(tfmd.created_date,"%Y-%m-%d") AS days_date,tfm.meter_name,tfm.address,tfm.pipe_diameter,tfm.canal_height,tfm.side_slop,tfm.channel_slop,tfm.m_r_coefficient,tfm.canal_bottom_width,tfm.canal_top_width FROM `tbl_flow_meter_data` as tfmd LEFT join tbl_flow_meter as tfm on tfm.id=tfmd.flow_meter_id WHERE tfmd.id=' . $id . ' and tfmd.flag=1 ORDER by tfmd.id DESC LIMIT 0,1';
echo '$query1 ' . $query1;
mysqli_set_charset($con, 'utf8');
$result1 = mysqli_query($con, $query1);
if (mysqli_num_rows($result1) > 0) {
    while ($row1 = mysqli_fetch_assoc($result1)) {

        $now = time(); // or your date as well
        $your_date = strtotime($row1["days_date"]);
        $datediff = $now - $your_date;
        $Days = round($datediff / (60 * 60 * 24));

//        echo '<br>Days ' . $Days;

        $canal_name = $row1["meter_name"];
        $address = $row1["address"];
        $created_date_new = $row1["created_date_new"];

        $water_level = $row1["water_level"]; // Example value in meters
//            $feet = metersToFeet($meters);
        $water_level_final = number_format($water_level, 3);
        $discharge = 0;

        $inputDatetime = $row1["created_date"]; // Your given datetime
//        echo '<br>inputDatetime ' . $inputDatetime . ' ';
        $currentDatetime = new DateTime(); // Current datetime
        $oneHourAgo = (clone $currentDatetime)->modify('-1 hour');

// Convert input string to DateTime object
        $input = new DateTime($inputDatetime);

// Check if input datetime is older than 1 hour
        if ($input <= $oneHourAgo) {
            echo "Given datetime is older than 1 hour.";

            $apikey = 'elbJ5jPEd9ztvnno';
            $senderid = 'RUDlNF';
//$numbers = '9978291903,9978133917';
            $numbers = '9978133917,9586800836';

            $message = "Canal Water Level and Flow information
 Canal Name: " . $canal_name . ", Location: " . $address . "
 Water Level: " . $water_level_final . " Meter, Discharge : " . $discharge . " Cusec/Sec
 Status: Stop,Time: " . $created_date_new . " RUDRA INFOTECH";
//            $message = "Canal Water Level and Flow information, Canal Name:" . $canal_name . ",Location: " . $address . ", Water Level: " . $water_level_final . " Meter, Discharge : " . $discharge . " Cusec/Sec, Status: Stop,Time: " . $created_date_new . " RUDRA INFOTECH";
////$message = "Check this out: $link";
//// URL-encode the message to handle special characters like ':' and '/'
            $encodedMessage = urlencode($message);
//
            echo '$message ' . $message;
//
            $url = "http://sms.osdigital.in/V2/http-api.php?apikey=$apikey&senderid=$senderid&number=$numbers&message=$encodedMessage&format=json";

// Initialize cURL session
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute and get response
            $response = curl_exec($ch);
            curl_close($ch);

// Show response
            echo $response;
        } else {
            echo "Given datetime is within the last 1 hour.";
        }
    }
}
?>
