Are there best practices for storing and retrieving opening hours data in PHP for dynamic display on a website?
When storing and retrieving opening hours data in PHP for dynamic display on a website, it is best to use a database to store the opening hours in a structured format. This allows for easy retrieval and manipulation of the data when displaying it on the website. Additionally, using PHP functions to format the opening hours for display can help ensure consistency and accuracy.
// Sample code for storing and retrieving opening hours data from a MySQL database
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Store opening hours data in the database
$sql = "INSERT INTO opening_hours (day, hours) VALUES ('Monday', '9:00 AM - 5:00 PM')";
$conn->query($sql);
// Retrieve opening hours data from the database
$sql = "SELECT * FROM opening_hours";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "Day: " . $row["day"]. " - Hours: " . $row["hours"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();