How to retrieve the current time in PHP and write it in…

You can use the date() function to get the current time and then write the obtained time into the database. Here is an example code:

$current_time = date('Y-m-d H:i:s'); // 获取当前时间
// 假设$db是一个已经连接的数据库对象
$sql = "INSERT INTO table_name (time_column) VALUES ('$current_time')";
$result = $db->query($sql);

if ($result) {
    echo "当前时间已成功写入数据库";
} else {
    echo "写入数据库失败";
}

In the example above, the current time is first obtained using the date(‘Y-m-d H:i:s’) function and stored in the $current_time variable. Then, a SQL statement is constructed to insert the current time into the time_column column in the database. Finally, the SQL statement is executed and the write result is verified.

bannerAds