How can PHP be used to automatically add a date to new entries in a script?
To automatically add a date to new entries in a script using PHP, you can use the date() function to get the current date and time and insert it into your database along with the new entry. This ensures that each new entry is timestamped with the date it was created.
// 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);
}
// Get current date and time
$date = date('Y-m-d H:i:s');
// Insert new entry with current date
$sql = "INSERT INTO entries (content, entry_date) VALUES ('New Entry', '$date')";
if ($conn->query($sql) === TRUE) {
echo "New entry added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();