Thursday, October 31, 2019

PHP Get current date and insert to MySql Database


0. Make sure field to be inserted is of date type:



1. Get current date in php:
date_default_timezone_set("UTC");
$now=new DateTime();
echo $now->getTimestamp();

2. Connect to database and insert using the following statement:

$sql = "INSERT INTO bins (Number, Text, Created, TTL, Property)
VALUES (".$number.", '".$text."','".date("Y-m-d",$now->getTimestamp())."', '1','Active')";


if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}