How can PHP be used to store the time in hours and minutes in a variable and then save it in a database?
To store the time in hours and minutes in a variable in PHP and save it to a database, you can use the PHP `date()` function to get the current time in the desired format. Then, you can assign this value to a variable and insert it into the database using SQL queries.
// Get the current time in hours and minutes
$time = date('H:i');
// Connect to your database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert the time into the database
$sql = "INSERT INTO table_name (time_column) VALUES ('$time')";
if ($conn->query($sql) === TRUE) {
echo "Time saved successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close the database connection
$conn->close();